| 18 | import sys |
| 19 | |
| 20 | class LOBLightningModule(pl.LightningModule): |
| 21 | def __init__( |
| 22 | self, |
| 23 | model, |
| 24 | experiment_id, |
| 25 | learning_rate, |
| 26 | general_hyperparameters, |
| 27 | model_hyperparameters, |
| 28 | ): |
| 29 | super().__init__() |
| 30 | self.model = model |
| 31 | self.experiment_id = experiment_id |
| 32 | self.learning_rate = learning_rate |
| 33 | self.general_hyperparameters = general_hyperparameters |
| 34 | self.model_hyperparameters = model_hyperparameters |
| 35 | |
| 36 | self.loss = nn.CrossEntropyLoss() |
| 37 | |
| 38 | self.training_accuracy = Accuracy(task="multiclass", num_classes=3) |
| 39 | self.training_f1 = F1Score(task="multiclass", num_classes=3, average="macro") |
| 40 | self.validation_accuracy = Accuracy(task="multiclass", num_classes=3) |
| 41 | self.validation_f1 = F1Score(task="multiclass", num_classes=3, average="macro") |
| 42 | |
| 43 | self.batch_loss_training = [] |
| 44 | self.batch_accuracies_training = [] |
| 45 | self.batch_f1_scores_training = [] |
| 46 | self.batch_loss_validation = [] |
| 47 | self.batch_accuracies_validation = [] |
| 48 | self.batch_f1_scores_validation = [] |
| 49 | self.batch_loss_test = [] |
| 50 | self.test_outputs = [] |
| 51 | self.test_targets = [] |
| 52 | self.test_probs = [] |
| 53 | |
| 54 | self.csv_path = f"{logger.find_save_path(experiment_id)}/metrics.csv" |
| 55 | |
| 56 | def forward(self, x): |
| 57 | return self.model(x) |
| 58 | |
| 59 | def training_step(self, batch, batch_idx): |
| 60 | inputs, targets = batch |
| 61 | logits = self.model(inputs) |
| 62 | loss = self.loss(logits, targets) |
| 63 | outputs = nn.functional.softmax(logits, dim=1) |
| 64 | outputs = torch.argmax(outputs, dim=1) |
| 65 | train_acc = self.training_accuracy(outputs, targets) |
| 66 | train_f1 = self.training_f1(outputs, targets) |
| 67 | |
| 68 | self.batch_loss_training.append(loss.item()) |
| 69 | self.batch_accuracies_training.append(train_acc.item()) |
| 70 | self.batch_f1_scores_training.append(train_f1.item()) |
| 71 | |
| 72 | return loss |
| 73 | |
| 74 | def validation_step(self, batch, batch_idx): |
| 75 | inputs, targets = batch |
| 76 | logits = self.model(inputs) |
| 77 | loss = self.loss(logits, targets) |