最近在学习Pytorch深度学习框架,由于初次接触,故记录一下学习的历程~
Author:qyan.li
Date:2021.11.17
nn.Sequential
个人简单理解:nn.Sequential
通过序列创建神经网络结构
1 Pytorch官方文档Doc:
A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an
OrderedDict
of modules can be passed in.2 解释说明:
nn.Sequential
本质上为序列化的容器,其中可存储各个模块,模块于容器中的顺序即为input输入其中用于输出output的顺序,同时支持字典的方式实现序列化容器的构建
import torch
from torch import nn
from collections import OrderedDict # 需导入OrderedDict,否则会报错
def Test():
# 此种方式仅可以使用索引访问某一层
Module1 = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
print(Module1)
print("----------------------------")
# 此种方式可借助于曾名访问某一层
Module2 = nn.Sequential(OrderedDict([
("con1",nn.Conv2d(1,20,5)),
("Relu1",nn.ReLU()),
("con2",nn.Conv2d(20,64,5)),
("Relu2",nn.ReLU())])
)
print(Module2)
if __name__ == '__main__':
Test()
'''output---
Sequential(
(0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
(1): ReLU()
(2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
(3): ReLU()
)
----------------------------
Sequential(
(con1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
(Relu1): ReLU()
(con2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
(Relu2): ReLU()
)
'''
参考文献:https://blog.csdn.net/qq_27825451/article/details/90551513
nn.Conv2d
Conv2d在pytorch中负责构建卷积层操作-convolution
1 Pytorch官方文档Doc:
class
torch.nn.``Conv2d
(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=‘zeros’, device=None, dtype=None)2 参数说明:
in_channels->输入维度
out_channels->输出维度
kernel_size->卷积和大小(可用于确定输出像素矩阵的宽度和高度)
stride = 1->卷积操作步长
padding = 0->补零操作(说明上下左右补零的个数)
padding = 1->input:33*33 output:35*35
padding_mode = ‘zeros’->周围补零
'''以图像为例说明:
num:输入图像的个数
dimension:每幅图像的维度:如RGB
width:像素矩阵宽度
height:像素矩阵高度
'''
import torch
from torch import nn
def Test():
# input:(num,dimension,width,height)
input = torch.randn(3,1,4,6);
# in_channels:input_dimension out_channels:output_dimension
conv = nn.Conv2d(in_channels = 1,out_channels = 100,kernel_size = (2,3))
# output(p,q) input(x,y) conv(m,n) p = x-m+1;q = n-y+1
output = conv(input)
print('input:',input.data)
print('output:',output.data)
if __name__ == '__main__':
Test()
'''output:
input: torch.Size([3, 1, 4, 6])
output: torch.Size([3, 100, 3, 4])
'''
参考文献: https://blog.csdn.net/qq_42079689/article/details/102642610
nn.BatchNorm2d
卷积处理之后,在非线性激活函数之前,添加BatchNorm
进行数据归一化处理,使其分布一致,将数据强行拉回方差为0,均值为1的正态分布上,一方面使得数据分布一致,另外一方面避免梯度消失
参考文献: https://blog.csdn.net/bigFatCat_Tom/article/details/91619977
Pytorch命令之nn.ReLu
ReLu函数在pytorch中负责构建非线性激活函数,保证网络的非线性特征
1 Pytorch官方文档Doc:
class
torch.nn.``ReLU
(inplace=False)2 参数说明:
inplace->True说明输入input会被改变,而False不会被改变
(inplace参数设置应考虑后续操作是否会对input产生需求)
import torch
from torch import nn
def Test():
# ReLu->max(0,x)
input = torch.randn(2,3)
relu = nn.ReLU(inplace = False)
output = relu(input)
print('input:',input.data)
print('output:',output.data)
if __name__ == '__main__':
Test()
'''output:
input: tensor([[ 1.1905, -0.5330, -1.0013],
[-0.7853, 1.6066, -1.0514]])
output: tensor([[1.1905, 0.0000, 0.0000],
[0.0000, 1.6066, 0.0000]])
'''
nn.MaxPool2d
nn.MaxPool2d
用于构建pytorch中池化操作,主要包括maxpooling,averagepooling
1 Pytorch官方文档Doc:
class
torch.nn.``MaxPool2d
(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)2 参数说明:
kernel_size->移动茶窗大小
stride->步长
padding->池化操作时周围是否补零
ceil_mode->True向上取整,False向下取整
import torch
from torch import nn
def Test():
# MaxPool2d3d or 4d
# (num,width,height)
input = torch.randn(3,2,4)
maxpooling = nn.MaxPool2d(kernel_size = (2,2),stride = 2)
output = maxpooling(input)
print('input:',input)
print('output:',output)
if __name__ == '__main__':
Test()
'''output:
input: tensor([[[-0.9491, 0.3658, -0.8858, 0.0594],
[ 1.8162, -0.6154, 0.1346, -1.7614]],
[[-1.3035, -0.6148, 0.9820, -1.0435],
[ 0.4091, -2.3076, 0.5005, 0.1074]],
[[ 0.8871, 0.9837, 0.2303, 0.4890],
[ 0.1333, 0.2272, 0.6926, 0.3049]]])
output: tensor([[[1.8162, 0.1346]],
[[0.4091, 0.9820]],
[[0.9837, 0.6926]]])
'''
Tips:输入必须为三维数据:torch.randn(3,2,4)->表明共有3个数据,数据大小宽度width = 2,长度为length = 4
原因:3d相较于2d多出的一维用于将2d的数据全部放置于统一的input中
否则会报错:RuntimeError: non-empty 3D or 4D (batch mode) tensor expected for input
nn.linear
nn.linear
主要用于构建神经网络架构中的全链接层,用于决定最终的输出
Pytorch官方文档Doc:
class
torch.nn.``Linear
(in_features, out_features, bias=True, device=None, dtype=None)参数说明:
in_features:输入向量的维度(size of each input sample)
out_features:输出向量的维度(size of each output sample)
bias:全链接层是否会学习到偏置(bias)
import torch
from torch import nn
from collections import OrderedDict
def Test():
input = torch.randn(100,256)
fc = nn.Linear(256,10)
output = fc(input)
print(output.size())
if __name__ == '__main__':
Test()
'''ouput---
input->torch.Size([100,256])
ouput->torch.Size([100, 10])
'''
out_features->不仅是输出的维度,同时也是分类问题中的类别数目(例如:手写数字识别问题中,输入图像经过全链接层会输出10维的数据,每个数据代表该数字属于0-9类别的概率)