-
Notifications
You must be signed in to change notification settings - Fork 164
/
5.2-part3-lightning-mlp.py
78 lines (59 loc) · 2.41 KB
/
5.2-part3-lightning-mlp.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
# Unit 5.2. Training a Multilayer Perceptron in PyTorch & Lightning
# Part 3. Training a Multilayer Perceptron in PyTorch using Lightning
import lightning as L
import torch
import torch.nn.functional as F
from shared_utilities import PyTorchMLP, compute_accuracy, get_dataset_loaders
from watermark import watermark
# LightningModule that receives a PyTorch model as input
class LightningModel(L.LightningModule):
def __init__(self, model, learning_rate):
super().__init__()
self.learning_rate = learning_rate
self.model = model
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
features, true_labels = batch
logits = self(features)
loss = F.cross_entropy(logits, true_labels)
self.log("train_loss", loss)
return loss # this is passed to the optimizer for training
def validation_step(self, batch, batch_idx):
features, true_labels = batch
logits = self(features)
loss = F.cross_entropy(logits, true_labels)
self.log("val_loss", loss, prog_bar=True)
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.parameters(), lr=self.learning_rate)
return optimizer
if __name__ == "__main__":
print(watermark(packages="torch,lightning", python=True))
print("Torch CUDA available?", torch.cuda.is_available())
train_loader, val_loader, test_loader = get_dataset_loaders()
pytorch_model = PyTorchMLP(num_features=784, num_classes=10)
lightning_model = LightningModel(model=pytorch_model, learning_rate=0.05)
trainer = L.Trainer(
max_epochs=10,
accelerator="auto", # set to "auto" or "gpu" to use GPUs if available
devices="auto", # Uses all available GPUs if applicable
)
trainer.fit(
model=lightning_model,
train_dataloaders=train_loader,
val_dataloaders=val_loader,
)
train_acc = compute_accuracy(pytorch_model, train_loader)
val_acc = compute_accuracy(pytorch_model, val_loader)
test_acc = compute_accuracy(pytorch_model, test_loader)
print(
f"Train Acc {train_acc*100:.2f}%"
f" | Val Acc {val_acc*100:.2f}%"
f" | Test Acc {test_acc*100:.2f}%"
)
PATH = "lightning.pt"
torch.save(pytorch_model.state_dict(), PATH)
# To load model:
# model = PyTorchMLP(num_features=784, num_classes=10)
# model.load_state_dict(torch.load(PATH))
# model.eval()