赞
踩
实现目标: input->卷积核计算->output stride是移动的距离(步长)
- 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]])
平面channel通道是1,batch size'的大小是1 数据维度是5*5 这里面conv2d(N,C,H,W)里面的四个是N就是batch size 也就是输入图片的数量,C就是通道数这只是一个二维张量所以通道为1,H就是高,W就是宽,所以是1,1,,5,5
- input=torch.reshape(input,(1,1,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)

- # padding 填充 padding=1 左边,右边,下边,上边 插入一行 空的地方 默认为0
- output3=F.conv2d(input,kernel,stride=1,padding=1) # padding 默认是0-不进行填充
- print(output3)

- 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))
- 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)
- #
- # padding 填充 padding=1 左边,右边,下边,上边 插入一行 空的地方 默认为0
- output3=F.conv2d(input,kernel,stride=1,padding=1) # padding 默认是0-不进行填充
- print(output3)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。