赞
踩
import torch from torch import nn from d2l import torch as d2l net = nn.Sequential( # 采用了11*11的卷积核来捕捉对象,因为原始输入数据比较大 #步幅为4 ,可减少输出的高度核宽度。 #输出通道为96,远大于Lenet #卷积之后大小的计算:H_2 = (H_1 + 2 * padding - kernel_size) / stride + 1,向下取整 # 因为这里我们用的数据是fahsion_mnist ,输入通道就为1 # [1,224,224] => [1,96,54,54] nn.Conv2d(in_channels=1,out_channels=96,kernel_size=11,stride=4,padding=1),nn.ReLU(), #池化后的大小计算公式:H_2 = (H1 + 2 * padding - kernel_size) / stride + 1 # [1,96,54,54] => [1,96,26,26] nn.MaxPool2d(kernel_size=3,stride=2), #减小卷积窗口大小,使用padding = 2 来保证输入与输出的宽高一致,且增大输出通道。 # [1,96,26,26] => [1,256,26,26] H2=(26 + 2*2 -5)/1 +1 nn.Conv2d(in_channels=96,out_channels=256,kernel_size=5,padding=2),nn.ReLU(), #[1,256,26,26] => [1,256,12,12] nn.MaxPool2d(kernel_size=3,stride=2), #三个卷积层 # [1,256,12,12] => [1,384,12,12] nn.Conv2d(256,384, kernel_size=3, padding=1),nn.ReLU(), # [1,384,12,12] => [1,384,12,12] nn.Conv2d(384,384, kernel_size=3, padding=1),nn.ReLU(), # [1,384,12,12] => [1,,256,12,12] nn.Conv2d(384,256, kernel_size=3, padding=1),nn.ReLU(), #[1,,256,12,12] => [1,256,5,5] 因为 H2 = (12 -3 )/2 +1 = 5 nn.MaxPool2d(kernel_size=3, stride=2), #输入数据打平,传送给全连接层. nn.Flatten(), # 256 * 5 * 5 = 6400 # 由于输入的数据过大,远远大于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) ) #加载数据集 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,d2l.try_gpu())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。