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
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 :)
Most helpful comment
yes! very happy with this. send a PR :)