在 CIFAR 10 上训练一个大型模型

这将在CIFAR 10上训练一个大型模型进行蒸馏

View Run

15import torch.nn as nn
16
17from labml import experiment, logger
18from labml.configs import option
19from labml_nn.experiments.cifar10 import CIFAR10Configs, CIFAR10VGGModel
20from labml_nn.normalization.batch_norm import BatchNorm

配置

我们使用CIFAR10Configs 它来定义所有与数据集相关的配置、优化器和训练循环。

23class Configs(CIFAR10Configs):
30    pass

适用于 CIFAR-10 分类的 VGG 样式模型

这源于通用的 VGG 风格架构

33class LargeModel(CIFAR10VGGModel):

创建卷积层和激活

40    def conv_block(self, in_channels, out_channels) -> nn.Module:
44        return nn.Sequential(

辍学

46            nn.Dropout(0.1),

卷积层

48            nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),

批量标准化

50            BatchNorm(out_channels, track_running_stats=False),

激活 ReLU

52            nn.ReLU(inplace=True),
53        )
55    def __init__(self):

使用给定的卷积大小(通道)创建模型

57        super().__init__([[64, 64], [128, 128], [256, 256, 256], [512, 512, 512], [512, 512, 512]])

创建模型

60@option(Configs.model)
61def _large_model(c: Configs):
65    return LargeModel().to(c.device)
68def main():

创建实验

70    experiment.create(name='cifar10', comment='large model')

创建配置

72    conf = Configs()

装载配置

74    experiment.configs(conf, {
75        'optimizer.optimizer': 'Adam',
76        'optimizer.learning_rate': 2.5e-4,
77        'is_save_models': True,
78        'epochs': 20,
79    })

设置保存/加载的模型

81    experiment.add_pytorch_models({'model': conf.model})

打印模型中参数的数量

83    logger.inspect(params=(sum(p.numel() for p in conf.model.parameters() if p.requires_grad)))

开始实验并运行训练循环

85    with experiment.start():
86        conf.run()

90if __name__ == '__main__':
91    main()