forked from Lightning-AI/dl-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_utilities.py
115 lines (90 loc) · 3.86 KB
/
shared_utilities.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import lightning as L
import torch
import torch.nn.functional as F
import torchmetrics
from torch.utils.data import DataLoader
from torch.utils.data.dataset import random_split
from torchvision import datasets, transforms
class PyTorchMLP(torch.nn.Module):
def __init__(self, num_features, num_classes):
super().__init__()
self.all_layers = torch.nn.Sequential(
# 1st hidden layer
torch.nn.Linear(num_features, 50),
torch.nn.ReLU(),
# 2nd hidden layer
torch.nn.Linear(50, 25),
torch.nn.ReLU(),
# output layer
torch.nn.Linear(25, num_classes),
)
def forward(self, x):
x = torch.flatten(x, start_dim=1)
logits = self.all_layers(x)
return logits
class LightningModel(L.LightningModule):
def __init__(self, model, learning_rate):
super().__init__()
self.learning_rate = learning_rate
self.model = model
self.train_acc = torchmetrics.Accuracy(task="multiclass", num_classes=10)
self.val_acc = torchmetrics.Accuracy(task="multiclass", num_classes=10)
self.test_acc = torchmetrics.Accuracy(task="multiclass", num_classes=10)
def forward(self, x):
return self.model(x)
def _shared_step(self, batch):
features, true_labels = batch
logits = self(features)
loss = F.cross_entropy(logits, true_labels)
predicted_labels = torch.argmax(logits, dim=1)
return loss, true_labels, predicted_labels
def training_step(self, batch, batch_idx):
loss, true_labels, predicted_labels = self._shared_step(batch)
self.log("train_loss", loss)
self.train_acc(predicted_labels, true_labels)
self.log(
"train_acc", self.train_acc, prog_bar=True, on_epoch=True, on_step=False
)
return loss
def validation_step(self, batch, batch_idx):
loss, true_labels, predicted_labels = self._shared_step(batch)
self.log("val_loss", loss, prog_bar=True)
self.val_acc(predicted_labels, true_labels)
self.log("val_acc", self.val_acc, prog_bar=True)
def test_step(self, batch, batch_idx):
_, true_labels, predicted_labels = self._shared_step(batch)
self.test_acc(predicted_labels, true_labels)
self.log("test_acc", self.test_acc)
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.parameters(), lr=self.learning_rate)
return optimizer
class MNISTDataModule(L.LightningDataModule):
def __init__(self, data_dir="./mnist", batch_size=64):
super().__init__()
self.data_dir = data_dir
self.batch_size = batch_size
def prepare_data(self):
# download
datasets.MNIST(self.data_dir, train=True, download=True)
datasets.MNIST(self.data_dir, train=False, download=True)
def setup(self, stage: str):
self.mnist_test = datasets.MNIST(
self.data_dir, transform=transforms.ToTensor(), train=False
)
self.mnist_predict = datasets.MNIST(
self.data_dir, transform=transforms.ToTensor(), train=False
)
mnist_full = datasets.MNIST(
self.data_dir, transform=transforms.ToTensor(), train=True
)
self.mnist_train, self.mnist_val = random_split(mnist_full, [55000, 5000], generator=torch.Generator().manual_seed(42))
def train_dataloader(self):
return DataLoader(
self.mnist_train, batch_size=self.batch_size, shuffle=True, drop_last=True
)
def val_dataloader(self):
return DataLoader(self.mnist_val, batch_size=self.batch_size, shuffle=False)
def test_dataloader(self):
return DataLoader(self.mnist_test, batch_size=self.batch_size, shuffle=False)
def predict_dataloader(self):
return DataLoader(self.mnist_predict, batch_size=self.batch_size, shuffle=False)