Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

lit_mnist.py 2.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. from argparse import ArgumentParser
  2. import torch
  3. import pytorch_lightning as pl
  4. from torch.nn import functional as F
  5. from torch.utils.data import DataLoader, random_split
  6. from torchvision.datasets.mnist import MNIST
  7. from torchvision import transforms
  8. class LitClassifier(pl.LightningModule):
  9. def __init__(self, hidden_dim=128, learning_rate=1e-3):
  10. super().__init__()
  11. self.save_hyperparameters()
  12. self.l1 = torch.nn.Linear(28 * 28, self.hparams.hidden_dim)
  13. self.l2 = torch.nn.Linear(self.hparams.hidden_dim, 10)
  14. def forward(self, x):
  15. x = x.view(x.size(0), -1)
  16. x = torch.relu(self.l1(x))
  17. x = torch.relu(self.l2(x))
  18. return x
  19. def training_step(self, batch, batch_idx):
  20. x, y = batch
  21. y_hat = self(x)
  22. loss = F.cross_entropy(y_hat, y)
  23. return loss
  24. def validation_step(self, batch, batch_idx):
  25. x, y = batch
  26. y_hat = self(x)
  27. loss = F.cross_entropy(y_hat, y)
  28. self.log('valid_loss', loss)
  29. def test_step(self, batch, batch_idx):
  30. x, y = batch
  31. y_hat = self(x)
  32. loss = F.cross_entropy(y_hat, y)
  33. self.log('test_loss', loss)
  34. def configure_optimizers(self):
  35. return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
  36. @staticmethod
  37. def add_model_specific_args(parent_parser):
  38. parser = ArgumentParser(parents=[parent_parser], add_help=False)
  39. parser.add_argument('--hidden_dim', type=int, default=128)
  40. parser.add_argument('--learning_rate', type=float, default=0.0001)
  41. return parser
  42. def cli_main():
  43. pl.seed_everything(1234)
  44. # ------------
  45. # args
  46. # ------------
  47. parser = ArgumentParser()
  48. parser.add_argument('--batch_size', default=32, type=int)
  49. parser = pl.Trainer.add_argparse_args(parser)
  50. parser = LitClassifier.add_model_specific_args(parser)
  51. args = parser.parse_args()
  52. # ------------
  53. # data
  54. # ------------
  55. dataset = MNIST('', train=True, download=True, transform=transforms.ToTensor())
  56. mnist_test = MNIST('', train=False, download=True, transform=transforms.ToTensor())
  57. mnist_train, mnist_val = random_split(dataset, [55000, 5000])
  58. train_loader = DataLoader(mnist_train, batch_size=args.batch_size)
  59. val_loader = DataLoader(mnist_val, batch_size=args.batch_size)
  60. test_loader = DataLoader(mnist_test, batch_size=args.batch_size)
  61. # ------------
  62. # model
  63. # ------------
  64. model = LitClassifier(args.hidden_dim, args.learning_rate)
  65. # ------------
  66. # training
  67. # ------------
  68. trainer = pl.Trainer.from_argparse_args(args)
  69. trainer.fit(model, train_loader, val_loader)
  70. # ------------
  71. # testing
  72. # ------------
  73. trainer.test(test_dataloaders=test_loader)
  74. if __name__ == '__main__':
  75. cli_main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...