Search code examples
deep-learningneural-networkpytorch

How to use conv2d in this case


I want to create an NN layer such that:

  • for the input of size 100 assume every 5 samples create "block"
  • the layer should compute let's say 3 values for every block
  • so the input/output sizes of this layer should be: 100 -> 20*3
  • every block of size 5 (and only this block) is fully connected to the result block of size 3

If I understand it correctly I can use Conv2d for this problem. But I'm not sure how to correctly choose conv2d parameters.

Is Conv2d suitable for this task? If so, what are the correct parameters? Is that

  • input channels = 100
  • output channels = 20*3
  • kernel = (5,1) ?

Solution

  • You can use either Conv2D or Conv1D. With the data shaped like batch x 100 x n_features you can use Conv1D with this setup:

    Input channels:  n_features
    Output channels: 3 * output_features
    kernel:          5
    strides:         5
    

    Thereby, the kernel is applied to 5 samples and generates 3 outputs. The values for n_features and output_features can be anything you like and might as well be 1. Setting the strides to 5 results in a non-overlapping convolution so that each block uniquely contributes to one output.