Numpy.convolve and conv1d in pytorch

Hi all, there is an option in numpy.convolve , ‘same’ mode, that result of convolution will be in the same size of input, is there something like this in pytorch?
size (output of convolution) = size (input)

for numpy.convolve can see Here

I don’t think there’s a built in same option to conv1d but you could slice the output of conv1d with your desired size.

1 Like

thanks @richard
I added some zeros at the end of my tensors but my result is differ from numpy.convolve, what do you mean by ‘Slice the output to desired size?’

Note that numpy applies a convolution, i.e. the filter gets flipped before the sliding operation, while Pytorch and other frameworks use a cross-correlation, i.e. without flipping the kernel.

1 Like

If you want the output to have the same size as the input in a similar way to numpy, then you need to add zero padding to both ends of the input.

1 Like

Are there any functions to achieve accurate convolve operation in pytorch exactly like numpy’s version (numpy.convolve — NumPy v1.19 Manual)? I am computing the convolution with two given vectors, the result is still different even I flipped the kernel for pytorch compare with “numpy convolve”. Thanks!

How large is the difference between both outputs?
Relative errors in the range ~1e-6 are expected due to the limited floating point precision in float32.