AlexNet引入了dropput层
代码
import torch
from torch import nn
from d2l import torch as d2l
net = nn.Sequential(
# 样本数为1,通道数为96,11x11的卷积核,步幅为4,减少输出的高度和深度。 LeNet的通道数才6,此处96,为什么要增加这么多通道呢?
nn.Conv2d(1,96,kernel_size=11,stride=4,padding=1),nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
# 减小卷积窗口,使用填充2使输出的高与宽一致,且增大输出通道数
nn.Conv2d(96,256,kernel_size=5,padding=2),nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
# 连续使用3个卷积层,通道数继续增加
nn.Conv2d(256,384,kernel_size=3,padding=1),nn.ReLU(),
nn.Conv2d(384,384,kernel_size=3,padding=1),nn.ReLU(),
nn.Conv2d(384,256,kernel_size=3,padding=1),nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Flatten(),
# 相对于LeNet,全连接增加了几倍,用dropout来减少过拟合
nn.Linear(6400,4096),nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096,4096),nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096,10)
)
# 样本数为1,通道数为1,224x224
X = torch.randn(1,1,224,224)
for layer in net:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t',X.shape)
Conv2d output shape: torch.Size([1, 96, 54, 54])
ReLU output shape: torch.Size([1, 96, 54, 54])
MaxPool2d output shape: torch.Size([1, 96, 26, 26])
Conv2d output shape: torch.Size([1, 256, 26, 26])
ReLU output shape: torch.Size([1, 256, 26, 26])
MaxPool2d output shape: torch.Size([1, 256, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 256, 12, 12])
ReLU output shape: torch.Size([1, 256, 12, 12])
MaxPool2d output shape: torch.Size([1, 256, 5, 5])
Flatten output shape: torch.Size([1, 6400])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 10])
# 读取数据集, fashion_mnist的图片是28x28,为了满足AlexNet的输出,resize为224x224,通常来说这样并不好
batch_size = 128
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size,resize=224)
# 训练
lr,num_epochs = 0.01,10
d2l.train_ch6(net,train_iter,test_iter,num_epochs,lr,device=d2l.try_gpu())
运行结果:
当训练轮数增加到20的结果
# 训练
lr,num_epochs = 0.01,20
d2l.train_ch6(net,train_iter,test_iter,num_epochs,lr,device=d2l.try_gpu())
结果提升了2点多。
改变模型的前两层,使模型可以直接输入28x28的图片
# 将输入由224变成28
net = nn.Sequential(
nn.Conv2d(1,96,kernel_size=5,padding=2),nn.ReLU(),
# 样本数为1,通道数为96,11x11的卷积核,步幅为4,减少输出的高度和深度。 LeNet的通道数才6,此处96,为什么要增加这么多通道呢?
nn.MaxPool2d(kernel_size=3,stride=1),
# 减小卷积窗口,使用填充2使输出的高与宽一致,且增大输出通道数
nn.Conv2d(96,256,kernel_size=5,padding=2),nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
# 连续使用3个卷积层,通道数继续增加
nn.Conv2d(256,384,kernel_size=3,padding=1),nn.ReLU(),
nn.Conv2d(384,384,kernel_size=3,padding=1),nn.ReLU(),
nn.Conv2d(384,256,kernel_size=3,padding=1),nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Flatten(),
# 相对于LeNet,全连接增加了几倍,用dropout来减少过拟合
nn.Linear(6400,4096),nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096,4096),nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096,10)
)
X = torch.randn(1,1,28,28)
for layer in net:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t',X.shape)
Conv2d output shape: torch.Size([1, 96, 28, 28])
ReLU output shape: torch.Size([1, 96, 28, 28])
MaxPool2d output shape: torch.Size([1, 96, 26, 26])
Conv2d output shape: torch.Size([1, 256, 26, 26])
ReLU output shape: torch.Size([1, 256, 26, 26])
MaxPool2d output shape: torch.Size([1, 256, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 256, 12, 12])
ReLU output shape: torch.Size([1, 256, 12, 12])
MaxPool2d output shape: torch.Size([1, 256, 5, 5])
Flatten output shape: torch.Size([1, 6400])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 10])
# 读取数据集, fashion_mnist的图片是28x28,为了满足AlexNet的输出,resize为224x224,通常来说这样并不好
batch_size = 128
# train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size,resize=224)
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)
# 训练
lr,num_epochs = 0.01,20
d2l.train_ch6(net,train_iter,test_iter,num_epochs,lr,device=d2l.try_gpu())
结果下降了1点左右