out_channels must be divisible by groups
Package:
torch
50580

Exception Class:
ValueError
Raise code
device=None,
dtype=None) -> None:
factory_kwargs = {'device': device, 'dtype': dtype}
super(_ConvNd, self).__init__()
if in_channels % groups != 0:
raise ValueError('in_channels must be divisible by groups')
if out_channels % groups != 0:
raise ValueError('out_channels must be divisible by groups')
valid_padding_strings = {'same', 'valid'}
if isinstance(padding, str):
if padding not in valid_padding_strings:
raise ValueError(
"Invalid padding string {!r}, should be one of {}".format(
padding, valid_padding_strings))
if padding == 'same' and any(s != 1 for s in stride):
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too!
We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.
Links to the raise (2)
https://github.com/pytorch/pytorch/blob/e56d3b023818f54553f2dc5d30b6b7aaf6b6a325/torch/nn/modules/conv.py#L86 https://github.com/pytorch/pytorch/blob/e56d3b023818f54553f2dc5d30b6b7aaf6b6a325/torch/nn/quantized/modules/conv.py#L53Ways to fix
According to the torch.nn.Conv2d
documentation, the in_channels
and out_channels
must both be divisible by groups.
This particular error is raised due to the fact that out_channels
i.e 33 is not divisible by groups
, i.e 2.
Steps to reproduce the error:
- Setup virtual environment
pip install --user pipenv
mkdir test_folder
cd test_folder
pipenv shell
- Install pytorch
pipenv install torch
- Sample code
from torch import nn
m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1),groups=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output)
The error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-f2265d559eed> in <module>()
1 from torch import nn
----> 2 m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1),groups=2)
3 input = torch.randn(20, 16, 50, 100)
4 output = m(input)
5 print(output)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode, device, dtype)
430 super(Conv2d, self).__init__(
431 in_channels, out_channels, kernel_size_, stride_, padding_, dilation_,
--> 432 False, _pair(0), groups, bias, padding_mode, **factory_kwargs)
433
434 def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation, transposed, output_padding, groups, bias, padding_mode, device, dtype)
84 raise ValueError('in_channels must be divisible by groups')
85 if out_channels % groups != 0:
---> 86 raise ValueError('out_channels must be divisible by groups')
87 valid_padding_strings = {'same', 'valid'}
88 if isinstance(padding, str):
ValueError: out_channels must be divisible by groups
Fix
The in_channels
and out_channels
are respectively 16 and 33. And the n_groups
should be a common factor of both parameters. In other words both in_channels
and out_channels
should be both divisible by groups.
Here even though the value 2 is valid with respect to the in_channels but the out_channels is not divisible by 2.
Therefore groups
should be set to 1.
from torch import nn
m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1),groups=1)
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output)
Output
tensor([[[[ 3.9506e-01, 1.6591e-01, -2.0387e-01, ..., 4.1805e-02, -3.2882e-01, 3.6727e-01], [ 4.0608e-01, 9.7733e-02, 5.2039e-01, ..., 3.5625e-02, 4.5199e-01, 5.8352e-01], [ 1.4240e-01, 3.0349e-01, 7.5488e-01, ..., -2.1551e-01, 5.5937e-01, 3.1656e-01], ..., [-7.8479e-02, -1.4105e-01, 1.6713e-01, ..., -4.0072e-01, 1.4017e+00, -6.4047e-01], [-4.0632e-01, 2.1231
N.B. This is truncated output
Add a possible fix
Please authorize to post fix