Convolve 1d to have same output size as input

scipy convolve has mode=‘same’ option which gives you the output with the same size as input, how do I set parameters like stride and padding to achive the same with torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) ?

For a stride and dilation of 1, the padding should be weight.size(2)//2 for an odd sized kernel on both sides and [weight.size(2)//2, weight.size(2)//2 -1] for an evenly sizes kernel.

thank you so much for this answer. However I need bigger stride, is there a formula considering this parameter?

For a more general formula, you could take a look at @rwightman’s median pooling implementation.
This would calculate the padding for a 2-dimensional input, so you could remove one spatial dim for your use case.

1 Like