import torch
from torch import nn
def comp_conv2d(conv2d,x):
# 在维度前面加上通道数和批量大小数1
x=x.reshape((1,1)+x.shape)
# 得到4维
y=conv2d(x)
# 把前面两维去掉
return y.reshape(y.shape[2:])
# padding填充为1,左右
conv2d = nn.Conv2d(1,1,kernel_size=3,padding=1)
x=torch.rand(size=(8,8))
comp_conv2d(conv2d,x).shape
# 上下2 左右1 填充
conv2d = nn.Conv2d(1,1,kernel_size=(5,3),padding=(2,1))
comp_conv2d(conv2d,x).shape
# stride=2表示步幅是2
conv2d = nn.Conv2d(1,1,kernel_size=3,padding=1,stride=2)
comp_conv2d(conv2d,x).shape
# 填充和步长都都有,且不规则
conv2d=nn.Conv2d(1,1,kernel_size=(3,5),padding=(0,1),stride=(3,4))
# 结果是(8-3+0+3)/3=2 (8-5+2+4)/4=2
comp_conv2d(conv2d,x).shape
标签:nn,填充,步幅,comp,shape,padding,pytorch,conv2d,size From: https://www.cnblogs.com/jinbb/p/17609391.html