votes up 6

in_channels must be divisible by groups

Package:
torch
github stars 50580
Exception Class:
ValueError

Raise code

                 bias: bool,
                 padding_mode: str,
                 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(
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 3 votes down

According to the torch.nn.Conv2d documentation, the in_channels and out_channels must both be divisible by groups.

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(1633, (35), stride=(21), padding=(42), dilation=(31),groups=2)
input = torch.randn(201650100)
output = m(input)
print(output)

The error:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-2-0e3b9c681f10> 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=3)
      3 input = torch.randn(20, 16, 50, 100)
      4 output = m(input)
      5 print(output)


1 frames
/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)
     82         super(_ConvNd, self).__init__()
     83         if in_channels % groups != 0:
---> 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')


ValueError: in_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 groups should be set to 1.

from torch import nn
m = nn.Conv2d(1633, (35), stride=(21), padding=(42), dilation=(31),groups=1)
input = torch.randn(201650100)
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

Jul 03, 2021 kellemnegasi answer
kellemnegasi 31.6k
votes up 1 votes down

Jul 03, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix