LeNet网络详解与pytorch实现

最近在学习B站一个up主的视频,很棒。故决定在学习过程中进行笔记整理和总结。(无它,自用自勉)

给出收藏的博主笔记及up主笔记,以便自己日后查找翻阅。

Pytorch中tensor(输入输出层)的通道排序为:[batch, channel, height, width]

Pytorch中的卷积、池化、输入输出层中参数的含义与位置如下图所示

1. 模型代码model.py

import torch.nn as nn
import torch.nn.functional as F
# torch.nn包用来构建神经网络

class LeNet(nn.Module):                     #定义一个模型,继承于nn.Module父类
    def __init__(self):
        super(LeNet, self).__init__()       # 多继承,调用父类函数
        self.conv1 = nn.Conv2d(3, 16, 5)    # 输入深度,卷积核个数,卷积核尺寸
        self.pool1 = nn.MaxPool2d(2, 2)     # 池化核尺寸为2*2
        self.conv2 = nn.Conv2d(16, 32, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(32*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):       # 输入数据,Tensor
        x = F.relu(self.conv1(x))    # input(3, 32, 32) output(16, 28, 28)
        x = self.pool1(x)            # output(16, 14, 14)
        x = F.relu(self.conv2(x))    # output(32, 10, 10)
        x = self.pool2(x)            # output(32, 5, 5)
        x = x.view(-1, 32*5*5)       # output(32*5*5)   展平操作,-1表示第一维度推理
        x = F.relu(self.fc1(x))      # output(120)
        x = F.relu(self.fc2(x))      # output(84)
        x = self.fc3(x)              # output(10) 
        # 最后一层为什么没有用softmax函数,内部已经有实现
        return x

2. 相关函数解析

2.1 卷积Conv2d()

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0,
  dilation=1, groups=1, bias=True, padding_mode='zeros')

重要参数说明:

  • in_channels:输入特征矩阵的深度。
  • out_channels:输出特征矩阵的深度,也即卷积核的个数,使用n个卷积核卷积后得到的特征矩阵的深度就是n
  • kernel_size:卷积核尺寸大小。(可为int型,如3,表示卷积核大小为3×3,也可为tuple类型,如(3,5)表示卷积核大小为3×5 【height×width】)
  • stride:卷积核的步长。默认为1,与kernel_size类似,可为int型,也
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值