Why does nn.Conv2d require in_channels?

I am new to PyTorch.
I see that nn.Conv2d function takes as argument a pytorch tensor x, and params as in_channels, along with others.

My question is that, since the function performs a 2D convolution, the in_channels would always be equal to the channel depth of the input tensor x, so why do we need to specify the same as a parameter?



But the depth of the input tensor x is only known when called forward
So…forwarding and initializing network simultaneously?

For defining the tensor of Conv2d’s parameters

1 Like

It is because nn.Conv2d in essence uses a 3d filter i.e filter_size x filter_size x input_channels. And as @alan_ayu pointed out, you need filter size, input channels and output channels to define Conv2d’s parameters.

1 Like

Additionally to what was said, have a look at CS231n where the convolution operation including all shapes is well explained. :slight_smile:


I believe every channel of let’s say an RGB image contribute to the overall properties of that image meaning all of the channels must be taken into account when performing the convolution process. Let’s say you just have one filter, a convolution process applies that filter into all of the 3 channels which will output 3 difference results of same dimension and those 3 results are added.

Thanks for your reply
Can we not define the filter size at runtime? Is that against standard pytorch practice?

Is there any way we could mimic the Keras Conv2D layer in PyTorch without the need to specify in_channels?

Yes, you can use nn.LazyConv2d.

1 Like