1.理论
1.1存在的问题
1.2NiN块
更正:此处是一个卷积层后跟两个11卷积层,沐神是将11卷积层看作全连接层了
1.3NiN架构
最后的这个全局平均池化层是在整个输入通道中取一个最大的,因此要求输入通道数等于类别数。
1.5总结
2.代码
import torch
from torch import nn
from d2l import torch as d2l
def nin_block(in_channels,out_channels,kernel_size,strides,padding):
return nn.Sequential(nn.Conv2d(in_channels,out_channels,kernel_size,strides,padding=padding),nn.ReLU(),
nn.Conv2d(out_channels,out_channels,kernel_size=1),nn.ReLU(),
nn.Conv2d(out_channels,out_channels,kernel_size=1),nn.ReLU())
net = nn.Sequential(
nin_block(1,96,kernel_size=11,strides=4,padding=0),nn.MaxPool2d(3,stride=2),
nin_block(96,256,kernel_size=5,strides=1,padding=2),nn.MaxPool2d(3,stride=2),
nin_block(256,384,kernel_size=3,strides=1,padding=1),nn.MaxPool2d(3,stride=2),nn.Dropout(0.5),
nin_block(384,10,kernel_size=3,strides=1,padding=1),nn.AdaptiveAvgPool2d((1,1)),nn.Flatten()
)
X = torch.rand(size=(1, 1, 224, 224))
for layer in net:
X = layer(X)
print(layer.__class__.__name__, 'output shape:\t', X.shape)
lr, num_epochs, batch_size = 0.1, 10, 256
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())
nn.Flatten():保留第一个维度,其他的维度展平
nn.AdaptiveAvgPool2d((1,1))将输入通道最大池化层为1*1大小