import torch
from torch import nn
from d2l import torch as d2l
# NiN块
def nin_block(in_channels, out_channels, kernel_size, strides, padding):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, strides, padding),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
nn.ReLU()
)
# NiN模型
net = nn.Sequential(
nin_block(1, 96, kernel_size=11, strides=4, padding=0),
nn.MaxPool2d(3, 2),
nin_block(96, 256, kernel_size=5, strides=1, padding=2),
nn.MaxPool2d(3, 2),
nin_block(256, 384, kernel_size=3, strides=1, padding=1),
nn.MaxPool2d(3, 2), nn.Dropout(0.5),
# 标签类别数是10
nin_block(384, 10, kernel_size=3, strides=1, padding=1),
nn.AdaptiveAvgPool2d((1, 1)),
# 将四维的输出转成二维的输出
nn.Flatten()
)
# 训练模型
lr, num_epochs, batch_size = 0.03, 50, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
d2l.plt.show()
Output
loss 0.662, train acc 0.800, test acc 0.798
2038.3 examples/sec on cuda:0
有一说一,NiN跑了50轮的表现还不如AlexNet十轮的表现…可能数据集选的不对吧