PyTorch Convolution `in_channels` and `out_channels` meaning?
Asked Answered
W

2

7

From the PyTorch documentation for Convolution, I see the function torch.nn.Conv1d requires users to pass the parameters in_channels and out_channels.

I know these refer to "input channels" and "output channels", but I am not sure what they mean in the context of a convolution. My guess is that in_channels is equivalent to the input features and out_channels is equivalent to the output features, but I am not sure.

Could someone explain what thse arguments refer to?

Wasteful answered 18/6, 2019 at 15:12 Comment(0)
I
5

Given a convolution of:

  • length m
  • over N input channels / signals / variables
  • outputting P channels / features / filters

you would use:

nn.Conv1d(in_channels=N, out_channels=P, kernel_size=m)

This is illustrated for 2d images below in Deep Learning with PyTorch (where the kernels are of size 3x3xN (where N=3 for an RGB image), and there are 5 such kernels for the 5 outputs desired):

enter image description here

Induce answered 24/2, 2021 at 11:37 Comment(1)
Oh! One output channel per kernel. That's why if we stack two conv2d layers, the 2nd layer's in_channels have to be equal to the 1st layer's out_channels.Lifton
E
2

in_Channels denotes the number of channels in the input image, while out_channels denotes the number of channels produced by the convolution. In the case of image data, the most common cases are grayscale images which will have one channel, black, or color images that will have three channels – red, green, and blue. out_channels is a matter of preference but there are some important things to note about it.

  • Firstly, a larger number of out_channels allows the layer to potentially learn more useful features about the input data, though this is not a hard rule.
  • Secondly, the size of your CNN is a function of the number of in_channels/out_channels in each layer of your network and the number of layers.
Estrus answered 18/6, 2019 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.