LastK7
LastK7

Reputation: 181

PyTorch Convolution `in_channels` and `out_channels` meaning?

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?

Upvotes: 7

Views: 12544

Answers (2)

iacob
iacob

Reputation: 24301

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

Upvotes: 5

Anubhav Singh
Anubhav Singh

Reputation: 8709

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.

Upvotes: 2

Related Questions