This commit is contained in:
hobogalaxy committed 2021-01-10 16:03:00 +01:00
1 parent 0030c94b17
commit 66097c4bf3
11 files changed
+162 -200

No files matched your search

@@ -2,7 +2,7 @@ from sklearn.metrics import accuracy_score
import pytorch_lightning as pl
import torch.nn.functional as F
import torch
from template_utils.initializers import init_optimizer
from template_utils.initializers import load_class
# import custom architectures
from pytorch_modules.architectures.simple_mnist import SimpleMNISTClassifier
@@ -11,23 +11,19 @@ from pytorch_modules.architectures.simple_mnist import SimpleMNISTClassifier
class LitModel(pl.LightningModule):
"""
This is example of lightning model for MNIST classification.
The path to model should be specified in your run config. (run_configs.yaml)
The 'hparams' dict contains model args specified in config. (run_configs.yaml)
This class enables you to specify what happens during training, validation and test step.
You can just remove 'validation_step()' or 'test_step()' methods if you don't want to have them during training.
To learn how to create lightning models visit:
https://pytorch-lightning.readthedocs.io/en/latest/lightning_module.html
"""
def __init__(self, optimizer_config, **hparams):
def __init__(self, model_config, optimizer_config):
super().__init__()
self.optimizer_config = optimizer_config
hparams = {**model_config["args"], **optimizer_config["args"]}
self.save_hyperparameters(hparams)
self.optimizer_config = optimizer_config
self.model = SimpleMNISTClassifier(hparams=hparams)
self.model = SimpleMNISTClassifier(hparams=self.hparams)
def forward(self, x):
return self.model(x)
@@ -41,11 +37,10 @@ class LitModel(pl.LightningModule):
# training metrics
preds = torch.argmax(logits, dim=1)
acc = accuracy_score(preds.cpu(), y.cpu())
self.log('train_loss', loss, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('train_acc', acc, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('train_loss', loss, on_step=False, on_epoch=True)
self.log('train_acc', acc, on_step=False, on_epoch=True, prog_bar=True)
# we can return here anything and then read it in some callback
return loss, acc, preds, y
return loss
# logic for a single validation step
def validation_step(self, batch, batch_idx):
@@ -56,10 +51,11 @@ class LitModel(pl.LightningModule):
# validation metrics
preds = torch.argmax(logits, dim=1)
acc = accuracy_score(preds.cpu(), y.cpu())
self.log('val_loss', loss, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('val_acc', acc, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('val_loss', loss, on_step=False, on_epoch=True)
self.log('val_acc', acc, on_step=False, on_epoch=True, prog_bar=True)
return loss, acc, preds, y
# we can return here anything and then read it in some callback
return loss, {"preds": preds, "acc": acc, "y": y}
# logic for a single testing step
def test_step(self, batch, batch_idx):
@@ -70,10 +66,11 @@ class LitModel(pl.LightningModule):
# test metrics
preds = torch.argmax(logits, dim=1)
acc = accuracy_score(preds.cpu(), y.cpu())
self.log('test_loss', loss, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('test_acc', acc, on_step=False, on_epoch=True, logger=True, prog_bar=True)
self.log('test_loss', loss, on_step=False, on_epoch=True)
self.log('test_acc', acc, on_step=False, on_epoch=True)
return loss
def configure_optimizers(self):
return init_optimizer(optimizer_config=self.optimizer_config, model=self)
Optimizer = load_class(self.optimizer_config["class"])
return Optimizer(self.parameters(), **self.optimizer_config["args"])