# Convolution element wise with pytorch

I have a tensor A of size [904,145] and B of size [904,1234]. Both tensor have 904 elements. I am looking to convolve element wise each element of A with their respective element of B. So basically convolve A[i,:] with B[i,:] for all i. How can I do that in pytorch, I tried multiple combination but everytime I end up with a size of [904,904,1234] (I assume it does each element to all other elements) when I expect a final size of [904,1234]

Hello and welcome!

Iām not sure what you mean by convolve in this case. Can you write a code example in NumPy of what you want to accomplish(can include loops)?

1 Like

Something like this.

``````import numpy as np
a = np.zeros((904, 1234))
b = np.zeros((904, 145))
c = np.zeros(a.shape)
for i in range(a.shape[0]):
c[i,:] = np.convolve(a[i,:],b[i,:], mode = 'same')
``````

But I would like to do it in torch in one line since 904 dimension will play the dimension of my batch size.

I think you may need `torchaudio` installed. Then you can do the following:

``````import torch
import torchaudio.functional as AF

a = torch.rand((904, 1234))
b = torch.rand((904, 145))
c = AF.convolve(a, b, "same")
print(c.size())
``````
1 Like

thanks it worked! I never heard of torchaudio before that :-o

1 Like