In pytorch, we use:

nn.conv2d(input_channel, output_channel, kernel_size)

in order to define the convolutional layers.

I understand that if the input is an image which has size $\text{width} \times \text{height} \times 3$ we would set the input_channel = 3. I am confused, however, what if I have a data set that has dimension: $3 \times 3 \times 30$ or $30 \times 4 \times 5$?

Which number should I use to define the input_channel for these?

Thanks in advance.


The defining factor is which dimensions you want your 2-dimensional convolution sweep over, e.g.:

  • In images, you want the 2D convolution to sweep over the height and width dimensions, and the extra dimension (the color space) is the channels; for grayscale images, you have a single channel.

  • In a spectrogram, you want the 2D convolution to sweep over the time and frequency dimensions. As there are no further dimensions, there is only one channel, like with grayscale images.

In the cases you propose, e.g. "3 * 3 * 30", if we want the 2D convolution to happen in the two first dimensions, then the number of input channels would be 30. If we wanted the 2D convolution to sweep over two other dimensions, then the remaining one would be the number of input channels. The same for "30 * 4 * 5".

We should note, however, that 2D convolutions follow a strict convention in the ordering of dimensions. As described in the pytorch documentation, the convention is $(N,C_{in},H,W)$, which means that we should rearrange the dimensions in our input tensor (e.g. with torch.Tensor.permute) to ensure that the dimensions over which we want the 2D convolution to sweep are in the correct order (i.e. the last 2 dimensions).

  • $\begingroup$ Thanks for your answer. I got your points. So, for example, if I got a data set has dimension of 30 * 3 * 2, if the 30 is the number frequency channel, 3 is the number of receiver antenna, and 2 is the number of transmitter antenna, if I want to extra the features over frequency channel and the receiver antennas, then I should set the parameters 'input channel' to 2, then the filter (has the same number of channel as the input channel) will swap over 30 * 3 matrix to get the features. Besides, the number of output channel is the number of kernels. Is my understanding correct? $\endgroup$ – ColinGuolin Dec 10 '20 at 15:57
  • $\begingroup$ Yes, that is correct. $\endgroup$ – noe Dec 10 '20 at 16:26
  • $\begingroup$ Please, consider upvoting the answer if you found it useful, and marking it as correct if deemed so. Alternatively, please considering describing what the answer is lacking or why you think it is not correct, so that it can be improved. $\endgroup$ – noe Dec 10 '20 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.