Commit ·
6b25542
1
Parent(s): 6e7f214
debug
Browse files- inference_lstm.py +20 -12
inference_lstm.py
CHANGED
|
@@ -94,17 +94,22 @@ if __name__ == "__main__":
|
|
| 94 |
|
| 95 |
outputs = model(input_ids, attention_mask=attention_mask)
|
| 96 |
probs = F.softmax(outputs, dim=1)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
all_predictions = np.append(all_predictions, predictions.cpu().numpy())
|
| 110 |
|
|
@@ -117,9 +122,12 @@ if __name__ == "__main__":
|
|
| 117 |
|
| 118 |
batch_count += 1
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
all_labels = all_labels.
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
# Print classification report
|
| 124 |
# Calculate accuracy, F1 score, recall, and precision
|
| 125 |
accuracy = metrics.accuracy_score(all_labels, all_predictions)
|
|
|
|
| 94 |
|
| 95 |
outputs = model(input_ids, attention_mask=attention_mask)
|
| 96 |
probs = F.softmax(outputs, dim=1)
|
| 97 |
+
if num_categories > 1:
|
| 98 |
+
batch_size, total_classes = outputs.shape
|
| 99 |
+
if total_classes % num_categories != 0:
|
| 100 |
+
raise ValueError(f"Error: Number of total classes in the batch must of divisible by {num_categories}")
|
| 101 |
|
| 102 |
+
classes_per_group = total_classes // num_categories
|
| 103 |
+
# Group every classes_per_group values along dim=1
|
| 104 |
+
reshaped = outputs.view(outputs.size(0), -1, classes_per_group) # shape: (batch, self., classes_per_group)
|
| 105 |
|
| 106 |
+
# Argmax over each group of classes_per_group
|
| 107 |
+
preds = reshaped.argmax(dim=-1)
|
| 108 |
+
predictions = torch.argmax(probs, dim=1)
|
| 109 |
+
else:
|
| 110 |
+
predictions = torch.argmax(probs, dim=1)
|
| 111 |
+
|
| 112 |
+
print("DEBUG: Prediction shape: ", predictions.shape)
|
| 113 |
|
| 114 |
all_predictions = np.append(all_predictions, predictions.cpu().numpy())
|
| 115 |
|
|
|
|
| 122 |
|
| 123 |
batch_count += 1
|
| 124 |
|
| 125 |
+
# Turn predictions and labels to 1D arrays
|
| 126 |
+
all_labels = all_labels.reshape(-1, 1)
|
| 127 |
+
all_labels = np.array([int(label) for label in all_labels])
|
| 128 |
+
all_predictions = all_predictions.reshape(-1, 1)
|
| 129 |
+
print("DEBUG: all_labels shape: ", all_labels.shape)
|
| 130 |
+
print("DEBUG: all_predictions shape: ", all_predictions.shape)
|
| 131 |
# Print classification report
|
| 132 |
# Calculate accuracy, F1 score, recall, and precision
|
| 133 |
accuracy = metrics.accuracy_score(all_labels, all_predictions)
|