Search code examples
deep-learningpytorchconv-neural-network

Conv1d Pytorch on the columns of a matrix


I would like to compute, in the most efficient way, the following operation:

cc = nn.Conv1d(1,1,3,1,1,bias=False)
U = torch.randn(1,1,10,10)
V = torch.zeros_like(U)
for i in range(10):
    V[:,:,:,i] = cc(U[:,:,:,i])

In other words, I would like to apply a 1d convolution on the columns of the input U. Of course the for loop is too slow and I am sure there is a more efficient way to solve the problem. However, I can not find any idea to achieve that.


Solution

  • You can apply a 2D convolution with a rectangular shape window ie. 1x3:

    conv = nn.Conv2d(1,1, kernel_size=(1,3), padding=1, bias=False)