Pytorch中的卷积操作

本文详细介绍了PyTorch中torch.nn.functional.conv2d函数的使用方法,包括输入参数、形状转换以及不同参数对卷积结果的影响,适合理解卷积操作的基础学习者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pytorch中有两个函数可以用来进行卷积操作:1)torch.nn中Conv2d;2)torch.nn.functional中的conv2d
今天主要介绍一下第二个函数。它的函数调用方式如下:

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

其中input代表的是输入图像/矩阵(这里限制输入必须是以[minibatch,channel,H,W]这样的shape输入)
weight代表的是卷积核(同样是要求以上面的shape输入)
stride代表的是卷积核在输入图像上移动的步长
padding代表的是进行卷积之前是否对输入图像进行边缘填充(默认不填充)
具体代码如下:

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)   # [5,5]
print(kernel.shape)  # [3,3]
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))

print(input.shape)    # [1,1,5,5]
print(kernel.shape)   # [1,1,3,3]

# stride=1,卷积核每次移动一个元素
output1 = F.conv2d(input, kernel, stride=1)
print("stride=1,不进行padding,卷积操作后:")
print(output1)
print(output1.shape)

# padding=1, 边界填充一格,元素设置为0,扩充完之后是一个7*7的矩阵

output2 = F.conv2d(input, kernel, stride=1, padding=1)
'''
[[0,0,0,0,0,0,0]
 [0,1,2,0,3,1,0],
 [0,0,1,2,3,1,0],
 [0,1,2,1,0,0,0],
 [0,5,2,3,1,1,0],
 [0,2,1,0,1,1,0]
 [0,0,0,0,0,0,0]]
'''
print("stride=1,padding=1,卷积操作后:")
print(output2)
print(output2.shape)

# stride=2,卷积核每次移动2个元素
output3 = F.conv2d(input, kernel, stride=2)
print("stride=2,不进行padding,卷积操作后:")
print(output3)
print(output3.shape)

运行上面的代码得到的结果:
在这里插入图片描述
因为conv2d的input以及weight都有输入限制,因此在进行卷积之前,使用了torch.reshape改变了矩阵的shape

要自定义卷积核并执行卷积操作,需要按照以下步骤进行: 1. 导入必要的库,包括PyTorch库和numpy库。 ```python import torch import numpy as np ``` 2. 定义卷积核的权重矩阵,可以手动创建或使用随机数生成器。然后将权重矩阵转换为PyTorch张量,以便在下一步中使用。 ```python kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]).astype(np.float32) weights = torch.from_numpy(kernel).unsqueeze(0).unsqueeze(0) ``` 3. 创建输入张量,将其转换为PyTorch张量,并使用unsqueeze函数将其扩展为4D张量。 ```python input_tensor = np.random.rand(1, 1, 5, 5).astype(np.float32) input = torch.from_numpy(input_tensor).unsqueeze(0) ``` 4. 使用PyTorch中的conv2d函数进行卷积操作。将输入张量和权重矩阵传递给该函数,并指定所需的卷积参数(如步长、边界填充和输出通道数)。 ```python output = torch.nn.functional.conv2d(input, weights, stride=1, padding=0) ``` 5. 输出结果。可以使用PyTorch张量的numpy函数将张量转换为同型的numpy数组,并使用它来输出卷积操作的结果。 ```python result = output.numpy() print(result) ``` 完整的示例代码如下所示: ```python import torch import numpy as np # 定义卷积核 kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]).astype(np.float32) # 将卷积核转换为PyTorch张量 weights = torch.from_numpy(kernel).unsqueeze(0).unsqueeze(0) # 创建输入张量 input_tensor = np.random.rand(1, 1, 5, 5).astype(np.float32) # 将输入张量转换为PyTorch张量并扩展为4D张量 input = torch.from_numpy(input_tensor).unsqueeze(0) # 执行卷积运算 output = torch.nn.functional.conv2d(input, weights, stride=1, padding=0) # 输出结果 result = output.numpy() print(result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值