今天学习卷积操作演示。
# 导入相关的库
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]
])
# 打印输入张量和卷积核张量的形状
print(input.shape)
print(kernel.shape)
# 将输入张量和卷积核张量进行形状变换
input = torch.reshape(input, [1, 1, 5, 5])
kernel = torch.reshape(kernel, (1, 1, 3, 3))
# 打印形状变换后的输入张量和卷积核张量形状
print(input.shape)
print(kernel.shape)
# conv2d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1):
# 这是PyTorch中的conv2d函数。参数的含义如下