1 卷积输出维度计算方法
卷积核输出维度计算_csdn_1HAO的博客-优快云博客_卷积核输出计算
2 torch.reshape用法
import torch
a=torch.tensor([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
print("a的shape:",a.shape)
b=torch.reshape(a,((4,3,1)))
print("b:",b)
print("b的shape:",b.shape)
a的shape: torch.Size([2, 2, 3])
b: tensor([[[ 1],
[ 2],
[ 3]],
[[ 4],
[ 5],
[ 6]],
[[ 7],
[ 8],
[ 9]],
[[10],
[11],
[12]]])
b的shape: torch.Size([4, 3, 1])
torch.reshape用法_江南汪的博客-优快云博客_torch.reshape
3 torch 中conv2d 的padding用法
参考
https://www.jianshu.com/p/a6da4ad8e8e7
# 定义一个1*1卷积,设置填充模式为'zeros',在高和宽维度上两边各填充1个单位
In [53]: conv_zeros = torch.nn.Conv2d(1,1,1,1,padding=1,padding_mode='zeros',bias=False)
# 查看定义的卷积
In [54]: conv_zeros
Out[54]: Conv2d(1, 1, kernel_size=(1, 1), stride=(1, 1), padding=(1, 1), bias=False)
# 将卷积核的权重设置为1,这样可使卷积后的输出即为填充后的输入矩阵
In [55]: conv_zeros.weight = torch.nn.Parameter(torch.ones(1,1,1,1))
# 查看卷积核权重
In [56]: conv_zeros.weight
Out[56]:
Parameter containing:
tensor([[[[1.]]]], requires_grad=True)
# 进行卷积计算,并输出结果
In [57]: conv_zeros(x)
Out[57]:
tensor([[[[ 0., 0., 0., 0., 0., 0.],
[ 0., 1., 2., 3., 4., 0.],
[ 0., 5., 6., 7., 8., 0.],
[ 0., 9., 10., 11., 12., 0.],
[ 0., 13., 14., 15., 16., 0.],
[ 0., 0., 0., 0., 0., 0.]]]], grad_fn=<ThnnConv2DBackward>)
4 torch.resize(dim1,dim2)
不能改变数据量多少,但
torch.resize_(dim1,dim2)
可以直接强制修改到指定维度