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__()
请注意,我们省略了 bias 参数
31 self.conv1 = nn.Conv2d(1, 20, 5, 1, bias=False)
具有 20 个通道的批量归一化(卷积层的输出)。此图层的输入将具有形状[batch_size, 20, height(24), width(24)]
34 self.bn1 = BatchNorm(20)
36 self.conv2 = nn.Conv2d(20, 50, 5, 1, bias=False)
使用 50 个通道进行批量归一化。此图层的输入将具有形状[batch_size, 50, height(8), width(8)]
39 self.bn2 = BatchNorm(50)
41 self.fc1 = nn.Linear(4 * 4 * 50, 500, bias=False)
使用 500 个通道进行批量归一化(完全连接层的输出)。此图层的输入将具有形状[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():
创建实验
71 experiment.create(name='mnist_batch_norm')
创建配置
73 conf = MNISTConfigs()
装载配置
75 experiment.configs(conf, {
76 'optimizer.optimizer': 'Adam',
77 'optimizer.learning_rate': 0.001,
78 })
开始实验并运行训练循环
80 with experiment.start():
81 conf.run()
85if __name__ == '__main__':
86 main()