Torch.conv2d with groups

I am trying to use torch.conv2d or torch.nn.functional.conv2d with groups specified, but it reports runtime error. The code is something like this:

a = torch.rand(4, 3, 8, 8)
b = torch.ones(3, 3, 3)
out = torch.conv2d(a, b, groups=3)

The error is something like this:

RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]

Changing torch.conv2d to torch.nn.functional.conv2d gives the same error.

The following code works:

a = torch.rand(4, 3, 8, 8)
b = torch.ones(3, 3, 3, 3)
out = torch.conv2d(a, b)

and the following also works:

a = torch.rand(4, 3, 8, 8)
b = torch.ones(3, 3, 3, 3)
out = torch.conv2d(a, b, groups=1)

The weight tensor is expected to have 4 dimensions as [out_channels, in_channels, height, width], which is the case for the second approach.

Thanks @ptrblck . The following code

a = torch.rand(4, 3, 8, 8)
b = torch.ones(3, 3, 3, 3)
out = torch.conv2d(a, b, groups=3)

gives this bug:

RuntimeError: Given groups=3, weight of size 3 3 3 3, expected input[4, 3, 8, 8] to have 9 channels, but got 3 channels instead

3 groups with a filter using in_channels=3 would need 9 channels.
The grouped conv section or the docs give you more information on the usage of the groups argument.

If you want to process each input channel separately, use:

input = torch.rand(4, 3, 8, 8)
weight = torch.ones(3, 1, 3, 3)
out = torch.conv2d(input, weight, groups=3)
1 Like

Thanks a lot. It is clear now. @ptrblck