本文结合小土堆的教学视频PyTorch深度学习快速入门教程进行学习
一、 基本骨架 Containers
1. torch.nn.Module
Module 类是所有神经网络模块的基类,还可以包含其他模块,允许他们嵌套在树状结构中。
当继承 Module 父类时,需要调用 __init__() 函数进行初始化处理,并且在该函数中必须调用父类的初始化函数。同时还可以调用 forward() 函数进行前向传播。
具体代码实现如下
import torch
# 继承Module模块,创建一个属于自己的类
class Model(torch.nn.Module):
# 类的初始化
def __init__(self):
super().__init__()
# 调用forward函数
def forward(self, input):
output = input + 1
return output
model = Model()
x = torch.tensor(1.0)
output = model(x)
print(output)
程序运行结果输出 tensor(2.)
2.torch.nn.Sequential
Sequential 类继承自Module 类,是一个有顺序的容器,将特定神经网络模块按照在传入容器的顺序依次执行
具体代码实现如下
from torch import nn
# 创建Sequential实例
model = nn.Sequential(
nn.Conv2d(1,20,5), # 第一次卷积操作
nn.ReLU(), # 第一次非线性激活
nn.Conv2d(20,64,5), # 第二次卷积操作
nn.ReLU() # 第二次非线性激活
)
print(model)
程序运行结果如下
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()
)
二、 卷积层 Convolution Layers
功能实现:提取输入图像中的特征信息
Conv2d:对二维数据进行卷积层的处理
1. torch.nn.functional.conv2d
函数定义:
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
参数含义:input 表示传入要进行处理的图像数据; weight 表示权重,即卷积核;bias 表示形状的可选偏置张量,默认值为 None; stride 表示卷积的步幅,默认值为1; padding 表示图像边沿填充的数目,默认值为0
具体代码实现如下
import torch
import torch.nn.functional as F
# 输入矩阵
input = torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]])
# 卷积核
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
# 由于直接使用torch.tensor传入的数据只有2个尺寸形式,而conv2d的传参要求需要4个尺寸形式
# 对输入矩阵和卷积核的尺寸进行转变
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# 输出卷积结果
output = F.conv2d(input,kernel,stride=1) # 卷积核步长为1,边缘无填充
output2 = F.conv2d(input,kernel,stride=2) # 卷积核步长为2,边缘无填充
output3 = F.conv2d(input,kernel,stride=1,padding=1) # 卷积核步长为1,边缘填充一圈
print(output)
print(output2)
print(output3)
程序运行结果如下
tensor([[[[10, 12, 12],
[18, 16, 16],
[13, 9, 3]]]])
tensor([[[[10, 12],
[13, 3]]]])
tensor([[[[ 1, 3, 4, 10, 8],
[ 5, 10, 12, 12, 6],
[ 7, 18, 16, 16, 8],
[11, 13, 9, 3, 4],
[14, 13, 9, 7, 4]]]])
2. torch.nn.Conv2d
计算公式为