Convolution operation in pytorch

How can I do convolution operation in Pytorch? (I mean: https://en.wikipedia.org/wiki/Kernel_(image_processing)#Convolution)

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: http://pytorch.org/docs/master/nn.html . Search for ‘convolution’.

2 Likes

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 (http://setosa.io/ev/image-kernels/) :

torch.sum(a*b)

OR (the better and more complete way)

import torch.nn.functional as F
from torch.autograd import Variable
F.conv2d(Variable(a.view(1,1,3,3)),Variable(b.view(1,1,3,3)))
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.