[英]How to fix error with pytorch conv2d function?
I am trying to use conv2d function on these two tensors:我正在尝试在这两个张量上使用 conv2d function :
Z = np.random.choice([0,1],size=(100,100))
Z = torch.from_numpy(Z).type(torch.FloatTensor)
print(Z)
tensor([[0., 0., 1., ..., 1., 0., 0.],
[1., 0., 1., ..., 1., 1., 1.],
[0., 0., 0., ..., 0., 1., 1.],
...,
[1., 0., 1., ..., 1., 1., 1.],
[1., 0., 1., ..., 0., 0., 0.],
[0., 1., 1., ..., 1., 0., 0.]
and和
filters = torch.tensor(np.array([[1,1,1],
[1,0,1],
[1,1,1]]), dtype=torch.float32)
print(filters)
tensor([[1., 1., 1.],
[1., 0., 1.],
[1., 1., 1.]])
But when I try to do torch.nn.functional.conv2d(Z,filters)
this error returns:但是当我尝试做torch.nn.functional.conv2d(Z,filters)
这个错误返回:
RuntimeError: weight should have at least three dimensions
I really don't understand what is the problem here.我真的不明白这里有什么问题。 How to fix it?如何解决?
The input to torch.nn.functional.conv2d(input, weight)
should be torch.nn.functional.conv2d(input, weight)
的输入应该是
You can use unsqueeze()
to add fake batch and channel dimensions thus having sizes: input: (1, 1, 100, 100)
and weight: (1, 1, 3, 3)
.您可以使用unsqueeze()
添加虚假批次和通道尺寸,从而具有尺寸:输入: (1, 1, 100, 100)
和重量: (1, 1, 3, 3)
。
torch.nn.functional.conv2d(Z.unsqueeze(0).unsqueeze(0), filters.unsqueeze(0).unsqueeze(0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.