nn.Conv2d out or input channel invalid combination of arguments

First of all, if this is a duplicate I am deeply sorry. Could not find the reason behind this behavior of torch nn.Conv2d. The following code results in invalid combination of argument - got (Tensor, int, int, int)

T = (4,4)
T_times = 10
test_layer = nn.Conv2d(in_channels=256, out_channels=T_times*(torch.prod(torch.tensor(T, dtype=torch.float))), kernel_size=1)

If I do the same operations with numpy as given below, there happens to be no problem. Is there anything I am missing?

test_layer = nn.Conv2d(in_channels=256, out_channels=T_times*(np.prod(T)), kernel_size=1)

Thanks in advance!


Have you tried adding a .item() to the result of the torch.prod()? I guess the semantic to translate tensors with a single element is not the same as numpy array with a single element.

Unfortunately the result is the same with the following one,
test_layer = nn.Conv2d(in_channels=256, out_channels=T_times*(torch.prod( torch.tensor(T, dtype=torch.float) ).item()), kernel_size=1)

I have also tried doing the ops seperately just to make sure I do not have a silly mistake but the result is the same. I thought the scalar thing could be a solution for this:) It is not cumbersome to do it in numpy, but adding a package for just this operation requires a strong heart.

I would print exactly what you pass for each argument and make sure nothing is a Tensor :slight_smile: Not sure which one it is at the moment, but a few prints in your code should make it clear.

LoL I have figured it out, my bad tbh. channels needs to be integer and a scalar. The solution is as follows if someone needs it,

test_layer = nn.Conv2d(in_channels=256, out_channels=T_times*(torch.prod(torch.tensor(T, dtype=torch.int)).item()), kernel_size=1)

But this error message tricked me at first when I tried without .item(). Or i did misunderstand it,

test_layer = nn.Conv2d(in_channels=256, out_channels=T_times*(torch.prod(torch.tensor(T, dtype=torch.int))), kernel_size=1)

Traceback (most recent call last):
File “/Applications/PyCharm.app/Contents/helpers/pydev/_pydevd_bundle/pydevd_exec2.py”, line 3, in Exec
exec(exp, global_vars, local_vars)
File “”, line 1, in
File “/Users/barisozcan/.virtualenvs/CapsuleNetworks/lib/python3.7/site-packages/torch/nn/modules/conv.py”, line 315, in init
False, _pair(0), groups, bias)
File “/Users/barisozcan/.virtualenvs/CapsuleNetworks/lib/python3.7/site-packages/torch/nn/modules/conv.py”, line 40, in init
self.bias = Parameter(torch.Tensor(out_channels))
TypeError: expected torch.FloatTensor (got torch.LongTensor)

I am not an expert but as an end user, I would prefer something indicating about float/int issue or tensor input.

Thank you for the insight :slight_smile:!

Glad you found the issue.

@smth should we open on issue to track better argument sanitation for Conv2D Module?

yea i think opening an issue, and then cleaning it up sounds like a good idea