栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch nn二位卷积conv2d操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch nn二位卷积conv2d操作

# TORCH.NN.FUNCTIONAL.CONV2D
# torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor

# 名词解释:
# input:  input tensor of shape (minibatch,in_channels,iH,iW) minibatch也就是最小的批量大小,
# in_channels为卷积的图片来源个数
# out_channels 为卷积核的个数
# iH 和 iW 是图片高度和宽度


# weight filters of shape (out_channels,in_channels/groups,kH,kW) 卷积大小,第二个参数的groups一般为1


# stride 步幅默认为1

# padding 在每一边都填充 默认为0
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])
#为什么要转变shape,因为functional的tensor格式为(x,x,x,x)

print(input.shape)
print(kernel.shape)
# 结果:
# torch.Size([5, 5])
# torch.Size([3, 3])
# 经过torch.reshape之后结果为:
# torch.Size([1, 1, 5, 5])
# torch.Size([1, 1, 3, 3])
output = F.conv2d(input,kernel,stride=1)
print(output)
# tensor([[[[10, 12, 12],
#           [18, 16, 16],
#           [13,  9,  3]]]])
output2 = F.conv2d(input,kernel,stride=2)
print(output2)
# tensor([[[[10, 12],
#           [13,  3]]]])
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
# tensor([[[[ 1,  3,  4, 10,  8],
#           [ 5, 10, 12, 12,  6],
#           [ 7, 18, 16, 16,  8],
#           [11, 13,  9,  3,  4],
#           [14, 13,  9,  7,  4]]]])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号