-
Notifications
You must be signed in to change notification settings - Fork 164
/
5.5-part2-datamodules.py
47 lines (34 loc) · 1.38 KB
/
5.5-part2-datamodules.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
# Unit 5.5. Organizing Your Data Loaders with Data Modules
import lightning as L
import torch
from shared_utilities import LightningModel, MNISTDataModule, PyTorchMLP
from watermark import watermark
if __name__ == "__main__":
print(watermark(packages="torch,lightning", python=True))
print("Torch CUDA available?", torch.cuda.is_available())
torch.manual_seed(123)
# NEW !!!
dm = MNISTDataModule()
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="cpu", devices="auto", deterministic=True
)
# NEW !!!
# trainer.fit(model=lightning_model,
# train_dataloaders=train_loader, val_dataloaders=val_loader)
trainer.fit(model=lightning_model, datamodule=dm)
train_acc = trainer.validate(dataloaders=dm.train_dataloader())[0]["val_acc"]
val_acc = trainer.validate(datamodule=dm)[0]["val_acc"]
test_acc = trainer.test(datamodule=dm)[0]["test_acc"]
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()