How to accomplish 2D convolution only in one dimension

I have a tensor like this,

and I want to do convolution operation such that my kernel height is 3 and width is also 3 but kernel moves only in the one direction (in the direction of width). I tried to use 2D convolution with stride 0 for Height but it throws an error as stride should be greater than zero. Any idea how to achieve this in pytorch?

One way could be is to do 1D convolution for each row (1,2,3) and the result of convolution from each row can be put together back in a tensor. Is there any easy way to achieve the same ?

I think what you are looking for is a 2D convolution with a 3x3 kernel size and without padding in the dimension you don’t want to traverse.

1 Like

Yes, with 2D convolution in PyTorch, it does what’s called “valid padding” by default. That is, it won’t go over the edges. And in this case, it won’t move vertically (up or down). The output you expect to get here, from a 3x9 input with a 3x3 kernel with stride 1, is a 1x7 output