Home > Enterprise >  How to fix error with pytorch conv2d function?
How to fix error with pytorch conv2d function?

Time:01-28

I am trying to use conv2d function on these two tensors:

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:

RuntimeError: weight should have at least three dimensions

I really don't understand what is the problem here. How to fix it?

CodePudding user response:

The input to torch.nn.functional.conv2d(input, weight) should be

enter image description here

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).

torch.nn.functional.conv2d(Z.unsqueeze(0).unsqueeze(0), filters.unsqueeze(0).unsqueeze(0))
  •  Tags:  
  • Related