6 卷积神经网络(LeNet)
6.1 LeNet
- LeNet-5由两个部分组成:
- 卷积编码器:由两个卷积层组成
- 全连接层密集块:由三个全连接层组成
- 每个卷积块中基本单位是:
- 卷积层(5x5卷积核)
- sigmoid激活函数
- 平均池化层
- 这些层将输入映射到多个二维特征输出,通常同时增加通道的数量
- 第一个卷积层有6个输出通道
- 第二个卷积层有16个输出通道
- 每个2x2pooling操作(stride=2)通过空间下采样将维数减少4倍
- 为了将卷积块的输出传递给稠密块,我们必须在小批量中展平每个样本,因此需要将四维输入转换成二维输入全连接层。
- LeNet的稠密块有三个全连接层,分别有120、84、10个输出。
1 | import torch |
Conv2d output shape: torch.Size([1, 6, 28, 28])
Sigmoid output shape: torch.Size([1, 6, 28, 28])
AvgPool2d output shape: torch.Size([1, 6, 14, 14])
Conv2d output shape: torch.Size([1, 16, 10, 10])
Sigmoid output shape: torch.Size([1, 16, 10, 10])
AvgPool2d output shape: torch.Size([1, 16, 5, 5])
Flatten output shape: torch.Size([1, 400])
Linear output shape: torch.Size([1, 120])
Sigmoid output shape: torch.Size([1, 120])
Linear output shape: torch.Size([1, 84])
Sigmoid output shape: torch.Size([1, 84])
Linear output shape: torch.Size([1, 10])
6.2 模型训练
- 在Fashion-MNIST上训练
1 | batch_size = 256 |
loss 1.000, train acc0.824,test acc0.804
6523.3 examples/sec on cpu