Skip to content

Commit

Permalink
Accuracy function modification to tensor comparisons in bert modeler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
advaithsrao committed Nov 26, 2023
1 parent ae52140 commit 1cea7e8
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions detector/modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def train(
# Forward pass
outputs = self.model(b_input_ids, attention_mask=b_input_mask, labels=b_labels)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

total_train_loss += loss.item()

Expand Down Expand Up @@ -180,11 +180,13 @@ def train(
with torch.no_grad():
outputs = self.model(b_input_ids, attention_mask=b_input_mask, labels=b_labels)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

total_eval_loss += loss.item()
logits = logits.detach().cpu().numpy()
label_ids = b_labels.detach().cpu().numpy()

# logits = logits.detach().cpu().numpy()
# label_ids = b_labels.detach().cpu().numpy()

total_eval_accuracy += self.accuracy(logits, label_ids)

avg_val_accuracy = total_eval_accuracy / len(validation_dataloader)
Expand Down Expand Up @@ -262,7 +264,7 @@ def predict(
with torch.no_grad():
outputs = self.model(b_input_ids, attention_mask=b_input_mask)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

_, prediction= torch.max(logits, dim=1)

Expand Down Expand Up @@ -294,13 +296,18 @@ def accuracy(
"""Calculates the accuracy of the model.
Args:
preds (np.array): The predictions of the model.
labels (np.array): The labels of the data.
preds (torch.Tensor|numpy.ndarray): The predictions of the model.
labels (torch.Tensor|numpy.ndarray): The labels of the data.
Returns:
float: The accuracy of the model.
"""

if isinstance(preds, np.ndarray):
preds = torch.from_numpy(preds)
if isinstance(labels, np.ndarray):
labels = torch.from_numpy(labels)

_, preds = torch.max(preds, dim=1)

return torch.tensor(torch.sum(preds == labels).item() / len(preds))
Expand Down Expand Up @@ -426,7 +433,7 @@ def train(
# Forward pass
outputs = self.model(b_input_ids, attention_mask=b_input_mask, labels=b_labels)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

total_train_loss += loss.item()

Expand Down Expand Up @@ -461,11 +468,13 @@ def train(
with torch.no_grad():
outputs = self.model(b_input_ids, attention_mask=b_input_mask, labels=b_labels)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

total_eval_loss += loss.item()
logits = logits.detach().cpu().numpy()
label_ids = b_labels.detach().cpu().numpy()

# logits = logits.detach().cpu().numpy()
# label_ids = b_labels.detach().cpu().numpy()

total_eval_accuracy += self.accuracy(logits, label_ids)

avg_val_accuracy = total_eval_accuracy / len(validation_dataloader)
Expand Down Expand Up @@ -543,7 +552,7 @@ def predict(
with torch.no_grad():
outputs = self.model(b_input_ids, attention_mask=b_input_mask)
loss = outputs[0]
logits = outputs[1]
logits = F.softmax(outputs[1], dim=1) # Taking the softmax of output

_, prediction= torch.max(logits, dim=1)

Expand Down Expand Up @@ -575,13 +584,18 @@ def accuracy(
"""Calculates the accuracy of the model.
Args:
preds (np.array): The predictions of the model.
labels (np.array): The labels of the data.
preds (torch.Tensor|numpy.ndarray): The predictions of the model.
labels (torch.Tensor|numpy.ndarray): The labels of the data.
Returns:
float: The accuracy of the model.
"""

if isinstance(preds, np.ndarray):
preds = torch.from_numpy(preds)
if isinstance(labels, np.ndarray):
labels = torch.from_numpy(labels)

_, preds = torch.max(preds, dim=1)

return torch.tensor(torch.sum(preds == labels).item() / len(preds))
Expand Down

0 comments on commit 1cea7e8

Please sign in to comment.