卷积运算是一种在信号处理、图像处理和神经网络等领域中广泛应用的数学运算。在图像处理和神经网络中,卷积运算可以用来提取特征、模糊图像、边缘检测等。在信号处理中,卷积运算可以用来实现滤波器等操作。
本系列实验使用如下环境
conda create -n DL python==3.11
conda activate DL
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
Tensor(张量)是PyTorch中用于表示多维数据的主要数据结构,类似于多维数组,可以存储和操作数字数据。
Tensor(张量)的维度(Dimensions)是指张量的轴数或阶数。在PyTorch中,可以使用size()方法获取张量的维度信息,使用dim()方法获取张量的轴数。
PyTorch中的张量可以具有不同的数据类型:
【深度学习】Pytorch 系列教程(一):PyTorch数据结构:1、Tensor(张量)及其维度(Dimensions)、数据类型(Data Types)
【深度学习】Pytorch 系列教程(二):PyTorch数据结构:1、Tensor(张量): GPU加速(GPU Acceleration)
PyTorch提供了丰富的操作函数,用于对Tensor进行各种操作,如数学运算、统计计算、张量变形、索引和切片等。这些操作函数能够高效地利用GPU进行并行计算,加速模型训练过程。
【深度学习】Pytorch 系列教程(三):PyTorch数据结构:2、张量的数学运算(1):向量运算(加减乘除、数乘、内积、外积、范数、广播机制)
【深度学习】Pytorch 系列教程(四):PyTorch数据结构:2、张量的数学运算(2):矩阵运算及其数学原理(基础运算、转置、行列式、迹、伴随矩阵、逆、特征值和特征向量)
【深度学习】Pytorch 系列教程(五):PyTorch数据结构:2、张量的数学运算(3):向量范数(0、1、2、p、无穷)、矩阵范数(弗罗贝尼乌斯、列和、行和、谱范数、核范数)与谱半径详解
【深度学习】Pytorch 系列教程(六):PyTorch数据结构:2、张量的数学运算(4):一维卷积及其数学原理(步长stride、零填充pad;宽卷积、窄卷积、等宽卷积;卷积运算与互相关运算)
【深度学习】Pytorch 系列教程(七):PyTorch数据结构:2、张量的数学运算(5):二维卷积及其数学原理
torch.matmul
:用于执行两个张量的矩阵乘法操作,它要求两个张量的维度需要满足矩阵乘法的规则,例如对于两个三维张量,torch.matmul
将在最后两个维度上执行矩阵乘法。import torch
# 创建两个张量
tensor1 = torch.randn(3, 4)
tensor2 = torch.randn(4, 5)
# 矩阵乘法
result = torch.matmul(tensor1, tensor2)
print(result.shape)
torch.mul
:用于对两个张量进行逐元素相乘,即*
运算符,会将两个张量的每个元素进行相乘。要求两个张量的形状需要一致或者满足广播规则。import torch
tensor1 = torch.tensor([[1, 2, 3],
[4, 5, 6]]) # shape: (2, 3)
tensor2 = torch.tensor([[7, 8],
[9, 10],
[11, 12]]) # shape: (3, 2)
# 使用 torch.matmul 进行矩阵乘法
result_matmul = torch.matmul(tensor1, tensor2) # 结果为 shape (2, 2)
print("Matmul result:")
print(result_matmul)
# 使用 torch.mul 进行逐元素相乘
result_mul = torch.mul(tensor1, tensor2.T) # 结果为逐元素相乘后的张量
print("\nMul result:")
print(result_mul)
import torch
tensor1 = torch.randn(3, 4, 5) # 维度为 (3, 4, 5)
tensor2 = torch.randn(3, 5, 6) # 维度为 (3, 5, 6)
result = torch.matmul(tensor1, tensor2)
print(result.size()) # 输出为 (3, 4, 6),说明两个张量进行了批量乘法
import torch
tensor1 = torch.tensor([[1, 2, 3],
[4, 5, 6]]) # shape: (2, 3)
tensor2 = torch.tensor([[7, 8],
[9, 10],
[11, 12]]) # shape: (3, 2)
tensor3 = torch.cat([tensor1, tensor1], dim=1)
# 通过 unsqueeze 添加新的维度来复制成三维张量
# tensor1_3d = tensor1.unsqueeze(0) # 在第一个维度上添加新的维度
# print(tensor1_3d.shape) # 输出:(1, 2, 3)
tensor1_3d = tensor1.expand(2, 2, 3) # 扩展维度
print(tensor1_3d.shape) # 输出:(2, 2, 3)
print(tensor1_3d)
result_matmul1 = torch.matmul(tensor1, tensor2)
print(f"{tensor1.size()}*{tensor2.size()}={result_matmul1.size()}")
print(result_matmul1)
result_matmul2 = torch.matmul(tensor1_3d, tensor2)
print(f"{tensor1_3d.size()}*{tensor2.size()}={result_matmul2.size()}")
print(result_matmul2)
result_matmul3 = torch.matmul(tensor2, tensor1)
print(f"{tensor2.size()}*{tensor1.size()}={result_matmul3.size()}")
print(result_matmul3)
result_matmul4 = torch.matmul(tensor2, tensor1_3d)
print(f"{tensor2.size()}*{tensor1_3d.size()}={result_matmul4.size()}")
print(result_matmul4)
import torch
import torch.nn.functional as F
# batch_size=2, channel=3, height=32, width=32
input_tensor = torch.randn(2, 3, 32, 32)
# out_channels=4, in_channels=3, kernel_height=3, kernel_width=3
conv_kernel = torch.randn(4, 3, 3, 3)
# 执行卷积操作
output = F.conv2d(input_tensor, conv_kernel, padding=1)
print(output.size()) # 输出为 (2, 4, 32, 32)
),这样才能进行逐通道的卷积操作。
),否则无法在输入张量上进行卷积操作。
- **步长**:卷积时的步长参数需要考虑输入张量的大小;
- **填充**:填充参数可以用来控制卷积操作的输出尺寸,用于保持输入和输出的尺寸一致。
import torch
import torch.nn.functional as F
#batch_size=2, channel=3, depth=10, height=32, width=32
input_tensor = torch.randn(2, 3, 10, 32, 32)
# out_channels=4, in_channels=3, kernel_depth=3, kernel_height=3, kernel_width=3
conv_kernel = torch.randn(4, 3, 3, 3, 3)
# 执行三维卷积操作
output = F.conv3d(input_tensor, conv_kernel, padding=1)
print(output.size()) # 输出为 (2, 4, 10, 32, 32)