How to set the Conv2d to have a common filter over all input channels

How to set the Conv2d layer to convolve one common filter (one filter per kernel) over all input channels rather than creating one per each input channel?

By default each filter in a conv layer will use all input channels and create a single output channel.
Could you describe your use case a bit more in case nn.Conv2d(in_channels, 1, ...) won’t work?

Thanks. There is no special use case as it is a part of another bigger project. I need a conv layer with different kernels and one filter per kernel to be shared between all input channels. The “nn.Conv2d(in_channels, 1, …)” will do the job however, I need to define multiples of this in case of having output_channels > 1.

Do I need to modify the C codes to implement this?

I’m not sure how to understand:

Filters and kernels are used as a synonym while it seems you put a different meaning to these terms. Could you explain what means what exactly?

Hi Moslem!

If I understand your use case correctly, you can reshape your input tensor
to “move” the channels dimension into the batch dimension, pass it into a
convolution with a single input channel, in_channels = 1, and then reshape
the output to restore the initial batch dimension. Something like this:

>>> import torch
>>> torch.__version__
>>> _ = torch.manual_seed (2023)
>>> conv = torch.nn.Conv2d (1, 1, 3)
>>> t = torch.randn (2, 3, 5, 5)
>>> conv (t.reshape (6, 1, 5, 5)).reshape (2, 3, 3, 3).shape
torch.Size([2, 3, 3, 3])


K. Frank

Sorry for the ambiguity in my description. @KFrank 's solution is roughly what I am looking for.

Thank you Frank. You saved me a lot of time.