-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_lit_mods.py
46 lines (38 loc) · 1.33 KB
/
base_lit_mods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from pytorch_lightning.core.lightning import LightningModule
import torch
from torch import nn
from pytorch_lightning.metrics import Accuracy
class BaseClassifierLitMod(LightningModule):
def __init__(self, lr=1e-3, weight_decay=0.0):
super().__init__()
self.lr = lr
self.wd = weight_decay
self.accuracy = Accuracy()
self.model = nn.Sequential(
nn.Flatten(),
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10),
)
def forward(self, x):
return self.model(x)
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.lr, weight_decay=self.wd)
return {
'optimizer': optimizer,
'monitor': 'val/loss'
}
def training_step(self, batch, batch_idx):
x, y = batch
logits = self.model(x)
loss = nn.functional.cross_entropy(logits, y)
self.log('train/loss', loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
logits = self.model(x)
loss = nn.functional.cross_entropy(logits, y)
accuracy = self.accuracy(nn.functional.softmax(logits, dim=1), y)
self.log('val/loss', loss, prog_bar=True)
self.log('val/accuracy', accuracy, prog_bar=True)
return loss