config makes more sense now
This commit is contained in:
1 parent
0ae8995eb3
commit
50872fd3c6
13 files changed
+214
-162
No files matched your search
@@ -0,0 +1 @@
|
||||
from .lightning_module import LitModel
|
||||
@@ -1,21 +0,0 @@
|
||||
hparams:
|
||||
max_epochs: 3
|
||||
batch_size: 64
|
||||
lr: 0.001
|
||||
weight_decay: 0.000001
|
||||
gradient_clip_val: 0.5
|
||||
accumulate_grad_batches: 1
|
||||
input_size: 784 # img size is 28*28*1
|
||||
output_size: 10
|
||||
lin1_size: 256
|
||||
lin2_size: 256
|
||||
lin3_size: 128
|
||||
|
||||
|
||||
resume_training:
|
||||
lightning_ckpt:
|
||||
resume_from_ckpt: False
|
||||
ckpt_path: "checkpoints/epoch=2.ckpt"
|
||||
wandb:
|
||||
resume_wandb_run: False
|
||||
wandb_run_id: "8uuomodb"
|
||||
@@ -0,0 +1 @@
|
||||
from .lightning_module import LitModel
|
||||
@@ -1,19 +0,0 @@
|
||||
hparams:
|
||||
max_epochs: 3
|
||||
batch_size: 64
|
||||
lr: 0.001
|
||||
weight_decay: 0.000001
|
||||
gradient_clip_val: 0.5
|
||||
accumulate_grad_batches: 1
|
||||
output_size: 10
|
||||
lin1_size: 256
|
||||
lin2_size: 256
|
||||
|
||||
|
||||
resume_training:
|
||||
lightning_ckpt:
|
||||
resume_from_ckpt: False
|
||||
ckpt_path: "checkpoints/epoch=2.ckpt"
|
||||
wandb:
|
||||
resume_wandb_run: False
|
||||
wandb_run_id: "8uuomodb"
|
||||
@@ -0,0 +1,70 @@
|
||||
from sklearn.metrics import accuracy_score
|
||||
import pytorch_lightning as pl
|
||||
import torch.nn.functional as F
|
||||
import torch
|
||||
|
||||
# custom models
|
||||
from models.transfer_learning_cifar10_classifier.models import *
|
||||
|
||||
|
||||
class LitModel(pl.LightningModule):
|
||||
|
||||
def __init__(self, hparams=None):
|
||||
super().__init__()
|
||||
|
||||
if hparams:
|
||||
self.save_hyperparameters(hparams)
|
||||
|
||||
self.model = EfficientNetPretrained(config=self.hparams)
|
||||
|
||||
def forward(self, x):
|
||||
return self.model(x)
|
||||
|
||||
# logic for a single training step
|
||||
def training_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# training metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
preds, y = preds.cpu(), y.cpu()
|
||||
acc = accuracy_score(preds, y)
|
||||
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)
|
||||
|
||||
return loss
|
||||
|
||||
# logic for a single validation step
|
||||
def validation_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# validation metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
preds, y = preds.cpu(), y.cpu()
|
||||
acc = accuracy_score(preds, y)
|
||||
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)
|
||||
|
||||
# we can return here anything and then read it in some callback
|
||||
return preds, y
|
||||
|
||||
# logic for a single testing step
|
||||
def test_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# test metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
preds, y = preds.cpu(), y.cpu()
|
||||
acc = accuracy_score(preds, y)
|
||||
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)
|
||||
|
||||
return loss
|
||||
|
||||
def configure_optimizers(self):
|
||||
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams["weight_decay"])
|
||||
Reference in new issue
Block a user