土堆学习笔记——P17卷积操作

文章介绍了如何使用PyTorch中的torch.nn和torch.nn.functional模块进行二维卷积操作,涉及stride、padding的概念以及它们对输出矩阵的影响。通过示例展示了不同参数设置下的conv2d函数应用。
摘要由CSDN通过智能技术生成

torch.nn 好像一个齿轮组,整体的运作
torch.nn.functional 就是里面的一个齿轮怎么转

在这里插入图片描述在这里插入图片描述stride=1,每次挪一个位置(横纵都是),stride可以是单个数字或一个元组(sH, sW),横向步径和纵向

在这里插入图片描述如果stride=2,那步径是2,得到2*2的矩阵
[[],[],[],…]表示二维矩阵

padding在给定矩阵左右两侧进行填充,padding=1,在左、右、上、下分别加1列/1行,加的值为0,

reshape(batchsize, 通道,矩阵(几维就几个数字))

代码如下

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]])#卷积核

input = torch.reshape(input, (1,1,5,5))#reshape(batchsize, 通道,矩阵5*5)
kernel = torch.reshape(kernel, (1,1,3,3))

print(input.shape)
print(kernel.shape)

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

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

output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值