卷积神经网络(LeNet)--基于pytorch+cpu

0.前言

    写于居寝静态管控的第三天上午,该死的疫情!

1.LeNet概述

    LeNet,它是最早发布的卷积神经网络之一,因其在计算机视觉任务中的高效性能而受到广泛关注。不包含输入层,其一共有7层,2个卷积层,2个池化层再加3个全连接层。 

    每个卷积层使用5×5卷积核和一个sigmoid激活函数和平均池化层。

    图片输入至网络之后,利用第一个卷积层把图片的特征提取分解,并增加至6个通道承载;第一个池化层缩小特征图片的维度,减少计算成本;第二个卷积层再一次把特征提取分解,并增加至16个通道;第二个池化层进一步缩小特征图片维度,进一步减少计算成本。

     考虑一下,最后输出是一个10维的数据,但是卷积块的输出是一个400维的输出,所以需要利用LeNet稠密块的三个全连接层,逐步将维度下降值120、84和10个输出。(这里我的理解好像差那么点点意思~)

2.代码实现Fashion-MNIST数据集

 2.1构建卷积神经网络

     实例化一个Sequential块把需要的层连接在一起,构建一个卷积神经网络。

(这里的Reshape我的理解是把无论输入什么样的图片都将它转换成一个28x28大小的输入)

    C1:5x5卷积核,上下左右填零2行(列)变成32x32大小,将通道数增加至6。

            输出featureMap的大小为:28x28 (32 - 5 + 1)

    P1:2x2平均采样核,采样步幅为2

            输出featureMap的大小为:14x14 (28 - 2 + 2)/ 2

    C2:5x5卷积核,将通道数增加至16。

            输出featureMap的大小为:10x10 (14 - 5 + 1)

    P2:2x2平均采样核,采样步幅为2

            输出featureMap的大小为: 5x5(10 - 2 + 2)/ 2

    nn.Flatten(): 默认将第0维保留下来,其余拍成一维

    后面三个全连接层:400➡120➡84➡10

    PS:每一层输出后必须添加激活函数将输出转变至非线性吧..........

import torch
from torch import nn
from d2l import torch as d2l

class Reshape(torch.nn.Module):
    def forward(self,x):
        return x.view(-1,1,28,28)

net = torch.nn.Sequential(
    Reshape(),
    nn.Conv2d(1,6,kernel_size = 5 , padding = 2),
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size = 2,stride = 2),
    nn.Conv2d(6,16,kernel_size = 5),
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size = 2,stride = 2),
    nn.Flatten(),
    nn.Linear(16*5*5,120),
    nn.Sigmoid(),
    nn.Linear(120,84),nn.Sigmoid(),
    nn.Linear(84,10)
)   

 2.2数据集

    导入Fashion-MNIST数据集,设置每个样本批量大小为256,载入数据的训练集和测试集。

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)

 2.3设置模型评估函数

    将网络设置为评估模式,计算数据预测正确的个数,返回正确/总数 = 正确率。

def evaluate_accuracy(net, data_iter): 
    if isinstance(net, nn.Module):
        net.eval()  # 设置为评估模式
    metric = d2l.Accumulator(2)  # 设置一个空的容器,用于存放:[0]正确预测的数量,[1]总预测的数量
    with torch.no_grad():
        for X, y in data_iter:
            metric.add(d2l.accuracy(net(X), y), y.numel()) #计算预测正确的数量
    return metric[0] / metric[1]  #返回正确率

 2.4设置模型训练函数

  2.4.1权重初始化+实例化工具

    这里的权重初始化跟第三章的用随机数初始化权重又不太一样了,emmm我后续再查一查xavier是如何初始化权重的吧。

    优化器选用随机梯度下降,损失函数选用交叉损失函数,Timer()不太清楚?,整体数据大小是训练集的大小。

def init_weights(m):  #初始化权重
        if type(m) == nn.Linear or type(m) == nn.Conv2d:
            nn.init.xavier_uniform_(m.weight)  #使用定义好的函数进行初始化
    net.apply(init_weights)  #把初始化权重应用于网络
    
    optimizer = torch.optim.SGD(net.parameters(), lr=lr)
    loss = nn.CrossEntropyLoss()
    timer, num_batches = d2l.Timer(), len(train_iter)

  2.4.2训练

    实例化一个空的容器,用于存放每一刷一遍训练集所有批量的训练损失、训练准确率以及总的样本数。将网络设置为训练模式,梯度归零,梯度下降,更新参数。

for epoch in range(num_epochs):
        # 训练损失之和,训练准确率之和,样本数
        metric = d2l.Accumulator(3)
        net.train()
        for i, (X, y) in enumerate(train_iter):
            timer.start()
            optimizer.zero_grad()  #梯度归零
            y_hat = net(X)
            l = loss(y_hat, y)
            l.backward()   #反向传播计算梯度
            optimizer.step()  #梯度下降更新参数

  2.4.3数据结果

    依次将训练损失之和,训练准确率以及样本总数放入容器。

    train_l : 训练损失总和 /样本总数 = 平均损失

    train_acc : 准确率总和/样本总数 = 平均准确率

    test_acc : 利用评估函数求取

    然后,依次打印训练集损失、训练集准确率、测试集准确率。

 with torch.no_grad():
                metric.add(l * X.shape[0], d2l.accuracy(y_hat, y), X.shape[0])
            timer.stop()
            train_l = metric[0] / metric[2]  #计算平均损失
            train_acc = metric[1] / metric[2]  #准确率
        test_acc = evaluate_accuracy(net, test_iter)
    print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
          f'test acc {test_acc:.3f}')

  2.4.5训练的全部代码

def train_ch6(net, train_iter, test_iter, num_epochs, lr):
    def init_weights(m):  #初始化权重
        if type(m) == nn.Linear or type(m) == nn.Conv2d:
            nn.init.xavier_uniform_(m.weight)  #使用定义好的函数进行初始化
    net.apply(init_weights)  #把初始化权重应用于网络
    optimizer = torch.optim.SGD(net.parameters(), lr=lr)
    loss = nn.CrossEntropyLoss()
    timer, num_batches = d2l.Timer(), len(train_iter)
    for epoch in range(num_epochs):
        # 训练损失之和,训练准确率之和,样本数
        metric = d2l.Accumulator(3)
        net.train()
        for i, (X, y) in enumerate(train_iter):
            timer.start()
            optimizer.zero_grad()  #梯度归零
            y_hat = net(X)
            l = loss(y_hat, y)
            l.backward()   #反向传播计算梯度
            optimizer.step()  #梯度下降更新参数
            with torch.no_grad():
                metric.add(l * X.shape[0], d2l.accuracy(y_hat, y), X.shape[0])
            timer.stop()
            train_l = metric[0] / metric[2]  #计算平均损失
            train_acc = metric[1] / metric[2]  #准确率
        test_acc = evaluate_accuracy(net, test_iter)
    print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
          f'test acc {test_acc:.3f}')

 2.5训练

    规定好学习率以及需要训练的次数,调用训练函数对参数进行训练。(还是建议大家用GPU来跑哈,CPU跑的时候我的风扇转的可大声了)。

lr, num_epochs = 0.9, 10
train_ch6(net, train_iter, test_iter, num_epochs, lr)

    看看打印的结果 

 2.6预测标签

    这里用一下第三章介绍的预测标签函数。

def predict_ch3(net, test_iter, n=6): 
    for X, y in test_iter:
        break
    trues = d2l.get_fashion_mnist_labels(y)
    preds = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1))
    titles = [true +'\n' + pred for true, pred in zip(trues, preds)]
    d2l.show_images(
        X[0:n].reshape((n, 28, 28)), 1, n, titles=titles[0:n])

predict_ch3(net, test_iter)

    看看预测结果

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值