p17 卷积操作

本文介绍了如何在PyTorch中使用`torch.nn.functional.conv2d`进行卷积操作,包括处理输入维度、参数如步长(stride)和填充(padding)的重要性,以及示例代码演示了不同参数设置的实际应用。
摘要由CSDN通过智能技术生成

介绍

import torch.nn.functional as F

这里function是nn的子包,但是很常用,一般单独导入。

然后介绍卷积操作:

​ 卷积操作目的是减少图片维度(减少了参数数量),保留关键信息。

在这里插入图片描述

具体操作是卷积核对位相乘作为结果,算完之后往后往下走。

这里不想多记。

然后代码:

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:", input)
print("kernel:", kernel)

print("input.shape:", input.shape)
print("kernel.shape:", kernel.shape)


#  要想用 torch.nn.functional.conv2d 这个函数,就必须满足形状的要求,上述的尺寸不满足,要做处理
#  上述的尺寸,只有input.shape: torch.Size([5, 5]), kernel.shape: torch.Size([3, 3]),并没有4个通道


input = torch.reshape(input, (1, 1, 5, 5))  # 注意这4个数字的意义,分别是:batch_size, in_channel, H, W , 变换形状之后,重新赋值给 input
kernel = torch.reshape(kernel, (1, 1, 3, 3))  # 注意这4个数字的意义,跟上面的不一样了

print("input.shape:", input.shape)
print("kernel.shape:", kernel.shape)

output = F.conv2d(input, kernel, stride=1)
print(output)

output2 = F.conv2d(input, kernel, stride=2)
print(output2)

output3 = F.conv2d(input, kernel, stride=1, padding=1)  # padding 设置的值,是往外扩充的行列数,值都是0,至于想要修改这个值,还有另外一个参数,一般不改
print(output3)

output4 = F.conv2d(input, kernel, stride=1, padding=0)  # padding 默认值是 0
print(output4)

注意的几个点:

​ 1.注意卷积要求的输入维度,参数要四个batch_size(批量大小),in_channel(输入通道数),高和宽

​ 2.参数说明:

​ stride 步长,即每次卷积运算完成走几个格子。

​ padding 填充,上下左右都填充,填充1即3x3变5x5

​ 图片辅助理解:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值