logo资料库

动手学深度学习(PyTorch实现)(七)–LeNet模型.pdf

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
动手学深度学习(PyTorch实现实现)(七七)–LeNet模型模型 动手学深度学习 LeNet模型模型1. LeNet模型2. PyTorch实现2.1 模型实现2.2 获取数据与训练 1. LeNet模型模型 LeNet分为卷积层块和全连接层块两个部分。下面我们分别介绍这两个模块。 卷积层块里的基本单位是卷积层后接平均池化层:卷积层用来识别图像里的空间模式,如线条和物体局部,之后的平均池化层则用来降低卷积层对位置的敏感性。 卷积层块由两个这样的基本单位重复堆叠构成。在卷积层块中,每个卷积层都使用5×55 \times 55×5的窗口,并在输出上使用sigmoid激活函数。第一个卷积层输出通道数为6,第 二个卷积层输出通道数则增加到16。 全连接层块含3个全连接层。它们的输出个数分别是120、84和10,其中10为输出的类别个数。 2. PyTorch实现实现 2.1 模型实现 模型实现 面我们通过Sequential类来实现LeNet模型。 # 导入相应的包 import sys sys.path.append("/home/kesci/input") import d2lzh1981 as d2l import torch import torch.nn as nn import torch.optim as optim import time # 展平操作,更改维度 class Flatten(torch.nn.Module): def forward(self, x): return x.view(x.shape[0], -1) # 将图像大小重定型 class Reshape(torch.nn.Module): def forward(self, x): return x.view(-1,1,28,28) #(B x C x H x W) # LeNet的实现 net = torch.nn.Sequential( # 重新定型图像大小 Reshape(), # 第一层卷积层,输入通道数1,输出通道数6,卷积核尺寸5*5,填充为2 nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2), #b*1*28*28 =>b*6*28*28 # 经过sigmoid激活函数 nn.Sigmoid(), # 平均池化层,核尺寸为2*2,步幅为2 nn.AvgPool2d(kernel_size=2, stride=2), #b*6*28*28 =>b*6*14*14 # 第二层卷积层,输入通道为6,输出通道为16,卷积核为5*5 nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5), #b*6*14*14 =>b*16*10*10 # 经过sigmoid激活函数 nn.Sigmoid(), # 平均池化层,核尺寸为2*2,步幅为2 nn.AvgPool2d(kernel_size=2, stride=2), #b*16*10*10 => b*16*5*5 # 展平操作 Flatten(), #b*16*5*5 => b*400 # 第一层全连接隐藏层,输入维度为16*5*5,输出维度为120 nn.Linear(in_features=16*5*5, out_features=120), # 经过sigmoid激活函数 nn.Sigmoid(), # 第二层全连接隐藏层,输入维度为120,输出维度为84 nn.Linear(120, 84), # 经过sigmoid激活函数 nn.Sigmoid(), # 第三层全连接输出层,输入维度为84,输出维度为10 nn.Linear(84, 10) ) 在LeNet中,在卷积层块中输入的高和宽在逐层减小。卷积层由于使用高和宽均为5的卷积核,从而将高和宽分别减小4,而池化层则将高和宽减半,但通道数则从1增加到16。全 连接层则逐层减少输出个数,直到变成图像的类别数10。
2.2 获取数据与训练 获取数据与训练 下面我们来实现LeNet模型。我们仍然使用Fashion-MNIST作为训练数据集。 # 数据批量数为256 batch_size = 256 # 获取训练数据与测试数据 train_iter, test_iter = d2l.load_data_fashion_mnist( batch_size=batch_size, root='/home/kesci/input/FashionMNIST2065') 选择GPU进行训练,如果没有GPU,仍然采用CPU进行训练 def try_gpu(): """If GPU is available, return torch.device as cuda:0; else return torch.device as cpu.""" if torch.cuda.is_available(): device = torch.device('cuda:0') else: device = torch.device('cpu') return device device = try_gpu() 我们实现evaluate_accuracy函数,该函数用于计算模型net在数据集data_iter上的准确率。 #计算准确率 ''' (1). net.train() 启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为True (2). net.eval() 不启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为False ''' def evaluate_accuracy(data_iter, net,device=torch.device('cpu')): """Evaluate accuracy of a model on the given data set.""" acc_sum,n = torch.tensor([0],dtype=torch.float32,device=device),0 for X,y in data_iter: # If device is the GPU, copy the data to the GPU. X,y = X.to(device),y.to(device) net.eval() with torch.no_grad(): y = y.long() acc_sum += torch.sum((torch.argmax(net(X), dim=1) == y)) #[[0.2 ,0.4 ,0.5 ,0.6 ,0.8] ,[ 0.1,0.2 ,0.4 ,0.3 ,0.1]] => [ 4 , 2 ] n += y.shape[0] return acc_sum.item()/n 我们定义函数train_ch5,用于训练模型。 ''' 参数含义: net: 要训练的网络 train_iter: 训练集 test_iter: 测试集 criterion: 损失函数 num_epochs: 训练周期 batch_size: 训练的小批量样本数 device: 训练使用的装置CPU或者GPU lr: 学习率 ''' def train_ch5(net, train_iter, test_iter,criterion, num_epochs, batch_size, device,lr=None): """Train and evaluate a model with CPU or GPU.""" print('training on', device) net.to(device) # 随机梯度下降为优化函数 optimizer = optim.SGD(net.parameters(), lr=lr) for epoch in range(num_epochs): # 初始化各种变量 train_l_sum = torch.tensor([0.0],dtype=torch.float32,device=device) train_acc_sum = torch.tensor([0.0],dtype=torch.float32,device=device) n, start = 0, time.time() # 开始训练 for X, y in train_iter: net.train() # 梯度参数清零 optimizer.zero_grad() X,y = X.to(device),y.to(device) # y_hat为网络的输出值 y_hat = net(X) # 计算损失 loss = criterion(y_hat, y) # 反向传播 loss.backward() # 更新参数 optimizer.step() with torch.no_grad(): # 转化为long型 y = y.long() # 计算损失的和
train_l_sum += loss.float() # 计算预测正确的个数 train_acc_sum += (torch.sum((torch.argmax(y_hat, dim=1) == y))).float() n += y.shape[0] test_acc = evaluate_accuracy(test_iter, net,device) print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, ' 'time %.1f sec' % (epoch + 1, train_l_sum/n, train_acc_sum/n, test_acc, time.time() - start)) 我们重新将模型参数初始化到对应的设备device(cpu or cuda:0)之上,并使用Xavier随机初始化。损失函数和训练算法则依然使用交叉熵损失函数和小批量随机梯度下降。 # 训练 lr, num_epochs = 0.9, 10 def init_weights(m): if type(m) == nn.Linear or type(m) == nn.Conv2d: torch.nn.init.xavier_uniform_(m.weight) net.apply(init_weights) net = net.to(device) #交叉熵描述了两个概率分布之间的距离,交叉熵越小说明两者之间越接近 criterion = nn.CrossEntropyLoss() train_ch5(net, train_iter, test_iter, criterion,num_epochs, batch_size,device, lr) 输出结果为: 对训练好的网络进行测试: # test for testdata,testlabe in test_iter: testdata,testlabe = testdata.to(device),testlabe.to(device) break print(testdata.shape,testlabe.shape) net.eval() y_pre = net(testdata) print(torch.argmax(y_pre,dim=1)[:10]) print(testlabe[:10]) 测试结果为: 作者:爱吃骨头的猫、
分享到:
收藏