Convolution operation in pytorch

How can I do convolution operation in Pytorch? (I mean:

I think torch.mul() is for elementwise multiplication right? So should I do torch.mul() followed by torch.sum() or is there any better way?

The pytorch docs are here: . Search for ‘convolution’.


To answer my own question in case anyone is interested:
Let’s say we have two tensor a, b:

  a= torch.rand(3,3)
  b= torch.rand(3,3)

We can write convolution for the middle pixel (a[1,1]) in two ways like what we see in lesson 1 ( :


OR (the better and more complete way)

import torch.nn.functional as F
from torch.autograd import Variable
1 Like

Thanks for the update @mefmef! Just to clarify, sum(a*b) isn’t a convolution - it’s a ‘dot product’. To make a convolution, you have to shift the kernel over every part of the whole image, like you see in the link you provided. At each point, you can use the sum(a*b) to calculate the value of the convolution at that point. In practice, that would be very slow, which is why we need to use the special pytorch function, which is optimized for the GPU.