12import torch.nn as nn
13import torch.nn.functional as F
14import torch.utils.data
15
16from labml import experiment
17from labml.configs import option
18from labml_helpers.module import Module
19from labml_nn.experiments.mnist import MNISTConfigs
20from labml_nn.normalization.batch_norm import BatchNorm
23class Model(Module):
28 def __init__(self):
29 super().__init__()
Note that we omit the bias parameter
31 self.conv1 = nn.Conv2d(1, 20, 5, 1, bias=False)
Batch normalization with 20 channels (output of convolution layer). The input to this layer will have shape [batch_size, 20, height(24), width(24)]
34 self.bn1 = BatchNorm(20)
36 self.conv2 = nn.Conv2d(20, 50, 5, 1, bias=False)
Batch normalization with 50 channels. The input to this layer will have shape [batch_size, 50, height(8), width(8)]
39 self.bn2 = BatchNorm(50)
41 self.fc1 = nn.Linear(4 * 4 * 50, 500, bias=False)
Batch normalization with 500 channels (output of fully connected layer). The input to this layer will have shape [batch_size, 500]
44 self.bn3 = BatchNorm(500)
46 self.fc2 = nn.Linear(500, 10)
48 def forward(self, x: torch.Tensor):
49 x = F.relu(self.bn1(self.conv1(x)))
50 x = F.max_pool2d(x, 2, 2)
51 x = F.relu(self.bn2(self.conv2(x)))
52 x = F.max_pool2d(x, 2, 2)
53 x = x.view(-1, 4 * 4 * 50)
54 x = F.relu(self.bn3(self.fc1(x)))
55 return self.fc2(x)
58@option(MNISTConfigs.model)
59def model(c: MNISTConfigs):
66 return Model().to(c.device)
69def main():
Create experiment
71 experiment.create(name='mnist_batch_norm')
Create configurations
73 conf = MNISTConfigs()
Load configurations
75 experiment.configs(conf, {
76 'optimizer.optimizer': 'Adam',
77 'optimizer.learning_rate': 0.001,
78 })
Start the experiment and run the training loop
80 with experiment.start():
81 conf.run()
85if __name__ == '__main__':
86 main()