Commit ·
c0ab709
1
Parent(s): e9a711a
- inference_lstm.py +2 -6
inference_lstm.py
CHANGED
|
@@ -104,15 +104,12 @@ if __name__ == "__main__":
|
|
| 104 |
reshaped = outputs.view(outputs.size(0), -1, classes_per_group) # shape: (batch, num_categories, classes_per_group)
|
| 105 |
probs = F.softmax(reshaped, dim=1)
|
| 106 |
# Argmax over each group of classes_per_group
|
| 107 |
-
print("DEBUG: Reshaped shape: ", reshaped.shape)
|
| 108 |
|
| 109 |
predictions = torch.argmax(probs, dim=-1)
|
| 110 |
else:
|
| 111 |
probs = F.softmax(outputs, dim=1)
|
| 112 |
predictions = torch.argmax(probs, dim=1)
|
| 113 |
|
| 114 |
-
print("DEBUG: Prediction shape: ", predictions.shape)
|
| 115 |
-
|
| 116 |
all_predictions = np.append(all_predictions, predictions.cpu().numpy())
|
| 117 |
|
| 118 |
if args.print_predictions:
|
|
@@ -128,8 +125,7 @@ if __name__ == "__main__":
|
|
| 128 |
all_labels = all_labels.reshape(-1, 1)
|
| 129 |
all_labels = np.array([int(label) for label in all_labels])
|
| 130 |
all_predictions = all_predictions.reshape(-1, 1)
|
| 131 |
-
|
| 132 |
-
print("DEBUG: all_predictions shape: ", all_predictions.shape)
|
| 133 |
# Print classification report
|
| 134 |
# Calculate accuracy, F1 score, recall, and precision
|
| 135 |
accuracy = metrics.accuracy_score(all_labels, all_predictions)
|
|
@@ -145,7 +141,7 @@ if __name__ == "__main__":
|
|
| 145 |
with open("predictions_lstm.txt", "w") as f:
|
| 146 |
for i in range(len(all_labels)):
|
| 147 |
idx = int(i)
|
| 148 |
-
f.write(f"Text: {test_dataset.get_text_(idx)}\n")
|
| 149 |
f.write(f"True Label: {all_labels[idx]}, Predicted Label: {all_predictions[idx]}\n")
|
| 150 |
f.write("\n")
|
| 151 |
|
|
|
|
| 104 |
reshaped = outputs.view(outputs.size(0), -1, classes_per_group) # shape: (batch, num_categories, classes_per_group)
|
| 105 |
probs = F.softmax(reshaped, dim=1)
|
| 106 |
# Argmax over each group of classes_per_group
|
|
|
|
| 107 |
|
| 108 |
predictions = torch.argmax(probs, dim=-1)
|
| 109 |
else:
|
| 110 |
probs = F.softmax(outputs, dim=1)
|
| 111 |
predictions = torch.argmax(probs, dim=1)
|
| 112 |
|
|
|
|
|
|
|
| 113 |
all_predictions = np.append(all_predictions, predictions.cpu().numpy())
|
| 114 |
|
| 115 |
if args.print_predictions:
|
|
|
|
| 125 |
all_labels = all_labels.reshape(-1, 1)
|
| 126 |
all_labels = np.array([int(label) for label in all_labels])
|
| 127 |
all_predictions = all_predictions.reshape(-1, 1)
|
| 128 |
+
|
|
|
|
| 129 |
# Print classification report
|
| 130 |
# Calculate accuracy, F1 score, recall, and precision
|
| 131 |
accuracy = metrics.accuracy_score(all_labels, all_predictions)
|
|
|
|
| 141 |
with open("predictions_lstm.txt", "w") as f:
|
| 142 |
for i in range(len(all_labels)):
|
| 143 |
idx = int(i)
|
| 144 |
+
f.write(f"Text: {test_dataset.get_text_(idx // 5)}\n")
|
| 145 |
f.write(f"True Label: {all_labels[idx]}, Predicted Label: {all_predictions[idx]}\n")
|
| 146 |
f.write("\n")
|
| 147 |
|