最近在学习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型,也