欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

pytorchnn二位卷积conv2d操作

时间:2023-06-03

# 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 在每一边都填充 默认为0import torchimport torch.nn.functional as Finput = 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]]]])

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。