Search code examples
pytorchtime-seriesconv-neural-networkpadding

How to apply padding to only one side in a PyTorch Conv1d Layer?


I am using PyTorch's CNN API to make a 1D convolutional layer. As an example, consider the below:

CNN = nn.Conv1d(
  in_channels=1,
  out_channels=1,
  kernel_size=10,
  padding=?
)

Ignore the padding component for now.

So this layer will take in a sequence of N elements (say N=100). And uses a sliding convolutional window of size 10 to output a sequence of size M.

I want to add padding to only the left side of the sequence so the output is exactly of size N-1.

I found that the formula left_hand_padding = kernel_size - 2 was able to output the correct padding that achieves this.

But I can't seem to find resources on how I can actually code this asymmetric padding. If I try to set the padding parameter in nn.Conv1d() to kernel_size - 2, both the right and left sides of the input are padded.

I also tried inputting a tuple (kernel_size - 2, 0), (element 0 is the LHS padding, element 1 is the RHS padding). But this gave me the error:

RuntimeError: expected padding to be a single integer value or a list of 1 values to match the convolution dimensions, but got padding=[8, 0]

Is this asymmetric padding just not supported?


Solution

  • You will need to pad it explicitly. Set the padding of convolution to 0 and use x = nn.funtional.pad(x, (kernel_size - 2, 0), ...). You can read more here.