文档地址:Module — PyTorch 2.4 documentation
1 nn.Module
新建一个名为test_nnMudule.py的文件
继承父类nn.Module
import torch
from torch import nnclass Net(nn.Module):def __init__(self):super(Net, self).__init__()def forward(self, x):y = 2*x +1return ynet = Net()
x1 = torch.tensor(1.0)
y1 = net.forward(x1)x2 = torch.tensor([1.0,2])
y2 = net.forward(x2)
print(y1)
print(y2)
运行结果
2 卷积层conv
针对二维图像的卷积:Conv2d — PyTorch 2.4 documentation
相关参数:
-
in_channels (int) – 输入图像中的通道数(例如RGB图像的通道数为3)
-
out_channels (int) – 卷积后产生的通道数(跟卷积核的数量相关)
-
kernel_size (int or tuple) – 卷积核大小
-
stride (int or tuple, optional) – 卷积的步长 (Default: 1)
-
padding (int, tuple or str, optional) – 对输入图像四个边的填充 Default: 0
实例:
import torchvision
from torch import nn
from torch.utils.data import dataloaderdata_transform = torchvision.transforms.ToTensor()#将PIL.Image转换为torch.FloatTensor
test_data = torchvision.datasets.CIFAR10('../dataset/cifar10', train=False, download=False, transform=data_transform)
dataloade = dataloader.DataLoader(test_data, batch_size=64, shuffle=True)
class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.conv1 = nn.Conv2d(3, 6, 3,stride=1)def forward(self, x):x = self.conv1(x)return xnet = Net()for data in dataloade:imgs, targets = dataprint(imgs.shape)output = net.forward(imgs)print(output.shape)break
假如,当前输入是大小为 n X n 的RGB图像(3, n ,n ),通道 C = 3
卷积核大小为 F X F,有 M 个卷积核,卷积的步长sterde为 S,padding为 P
计算公式为 A = [(n + 2P - F) / S] +1
经过卷积后的输出为:(M , A , A)
3 池化pooling
文档地址:MaxPool2d — PyTorch 2.4 documentation
相关参数:
-
kernel_size (Union[int, Tuple[int, int]]) – the size of the window to take a max over
-
stride (Union[int, Tuple[int, int]]) – the stride of the window. Default value is
kernel_size
-
padding (Union[int, Tuple[int, int]]) – Implicit negative infinity padding to be added on both sides
-
ceil_mode (bool) – 当为True时,将使用ceil来计算输出形状,而不是floor
举例:
当一个5X5的矩阵使用3X3的池化核来进行池化时,ceil_mode 的取值不同则对应的输出可能也会不同
实例:
import torch
from torch import nn
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]
], dtype=torch.float32)
print(input.shape)
input = input.reshape(-1, 1, 5, 5)#-1表示自动计算
# 这四个数字分别代表batch_size, channel, height, width
print(input.shape)class net(nn.Module):def __init__(self):super(net, self).__init__()self.maxpool1 = nn.MaxPool2d(2, 2)def forward(self, x):y = self.maxpool1(x)return ynet = net()
output = net.forward(input)
4 非线性激活
文档地址:torch.nn — PyTorch 2.4 documentation
常见的激活函数:relu,sigmoid
实例:
import torch
import torchvision
from torch.utils.data import DataLoaderinput = torch.tensor([[1, -0.5],[-1, 3]])
data_transform = torchvision.transforms.ToTensor()
dataset = torchvision.datasets.CIFAR10('../dataset/cifar10', train=False, download=False, transform=data_transform)dataloader = DataLoader(dataset, batch_size=64, shuffle=True)class net(torch.nn.Module):def __init__(self):super(net, self).__init__()self.relu = torch.nn.ReLU()def forward(self, x):y = self.relu(x)return ynet = net()