12import torch.nn as nn
13
14from labml import experiment
15from labml.configs import option
16from labml_helpers.module import Module
17from labml_nn.experiments.cifar10 import CIFAR10Configs
18from labml_nn.normalization.group_norm import GroupNorm21class Model(Module):26 def __init__(self, groups: int = 32):
27 super().__init__()
28 layers = []RGBනාලිකා
30 in_channels = 3එක්එක් කොටසෙහි එක් එක් ස්ථරයේ නාලිකා ගණන
32 for block in [[64, 64], [128, 128], [256, 256, 256], [512, 512, 512], [512, 512, 512]]:සම්මුතිය, සාමාන්යකරණය සහ සක්රිය කිරීමේ ස්ථර
34 for channels in block:
35 layers += [nn.Conv2d(in_channels, channels, kernel_size=3, padding=1),
36 GroupNorm(groups, channels),
37 nn.ReLU(inplace=True)]
38 in_channels = channelsඑක්එක් කොටස අවසානයේ මැක්ස් තටාක
40 layers += [nn.MaxPool2d(kernel_size=2, stride=2)]ස්ථරසමඟ අනුක්රමික ආකෘතියක් සාදන්න
43 self.layers = nn.Sequential(*layers)අවසන්පිවිසුම් ස්ථරය
45 self.fc = nn.Linear(512, 10)47 def forward(self, x):VGGස්ථර
49 x = self.layers(x)වර්ගීකරණස්ථරය සඳහා නැවත සකස් කරන්න
51 x = x.view(x.shape[0], -1)අවසානරේඛීය ස්ථරය
53 return self.fc(x)56class Configs(CIFAR10Configs):කණ්ඩායම්ගණන
58 groups: int = 1661@option(Configs.model)
62def model(c: Configs):66 return Model(c.groups).to(c.device)69def main():අත්හදාබැලීම සාදන්න
71 experiment.create(name='cifar10', comment='group norm')වින්යාසයන්සාදන්න
73 conf = Configs()වින්යාසයන්පූරණය කරන්න
75 experiment.configs(conf, {
76 'optimizer.optimizer': 'Adam',
77 'optimizer.learning_rate': 2.5e-4,
78 })අත්හදාබැලීම ආරම්භ කර පුහුණු ලූපය ක්රියාත්මක කරන්න
80 with experiment.start():
81 conf.run()85if __name__ == '__main__':
86 main()