forked from Lightning-AI/deep-learning-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
classifier.py
62 lines (47 loc) · 1.95 KB
/
classifier.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import hydra
import pytorch_lightning as pl
import torch
from torch.nn import functional as F
class LitClassifier(pl.LightningModule):
def __init__(self, optim, input_dim, output_dim, hidden_dim=128, **kwargs):
super().__init__()
self.save_hyperparameters()
self.l1 = torch.nn.Linear(input_dim, hidden_dim)
self.l2 = torch.nn.Linear(hidden_dim, output_dim)
self.val_accuracy = pl.metrics.Accuracy()
self.test_accuracy = pl.metrics.Accuracy()
self.example_input_array = torch.randn([input_dim, input_dim])
def forward(self, x):
x = x.view(x.size(0), -1)
x = torch.relu(self.l1(x))
x = torch.relu(self.l2(x))
return x
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.log("loss/train", loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.log('loss/val', loss)
accuracy = self.val_accuracy(torch.softmax(y_hat, dim=1), y)
self.log('accuracy/val', accuracy)
def validation_epoch_end(self, outputs):
self.log("accuracy/val", self.val_accuracy.compute())
def test_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.log('loss/test', loss)
test_accuracy = self.test_accuracy(torch.softmax(y_hat, dim=1), y)
return test_accuracy
def test_epoch_end(self, outputs):
self.log('accuracy/test', self.test_accuracy.compute())
def configure_optimizers(self):
return hydra.utils.instantiate(self.hparams.optim, params=self.parameters())
def on_train_start(self):
# Proper logging of hyperparams and metrics in TB
self.logger.log_hyperparams(self.hparams, {"loss/val": 0, "accuracy/val": 0, "accuracy/test": 0})