Pytorch: giving a 3d input to Conv2d gives a bad error message

Created on 21 May 2017  ·  3Comments  ·  Source: pytorch/pytorch

import torch
import torch.nn as nn
from torch.autograd import Variable

m = Variable(torch.randn(3, 10, 20))
nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3)(m)
Traceback (most recent call last):
  File "too.py", line 6, in <module>
    nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3)(m)
  File "/home/soumith/code/pytorch/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/soumith/code/pytorch/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)
  File "/home/soumith/code/pytorch/torch/nn/functional.py", line 43, in conv2d
    return f(input, weight, bias)
RuntimeError: expected 3D tensor
medium priority (this tag is deprecated)

Most helpful comment

yes! very happy with this. send a PR :)

All 3 comments

just encountered a similar problem using data with shape (batch_size, height, weight), added 1 dimension at position 1 and problem sorted. PS. I'm using in_channels = 1.

The error occurs because the call to view4d in torch/csrc/autograd/functions/convolution.cpp expects the weight tensor to also be 3D.

However, the (3D) input will be converted to a 4D tensor with shape (3, 10, 1, 20), even if we don't try to unsqueeze the weight tensor, the sizes will be mismatched.

Perhaps we should add a check at the Python level (in nn.functional.conv2d) to raise if input.dim() != 4?

@soumith if you're happy with this, I can send a PR

yes! very happy with this. send a PR :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajarshd picture rajarshd  ·  3Comments

bartvm picture bartvm  ·  3Comments

negrinho picture negrinho  ·  3Comments

soumith picture soumith  ·  3Comments

dablyo picture dablyo  ·  3Comments