PyTorch replace torch.nn.Conv2d with torch.nn.functional.conv2d

So I have this MNIST example for PyTorch.
I wanted to replace conv2d with functional method. But got unexpected error.

I replace self.conv1 = nn.Conv2d(1, 32, 5, padding=2) with self.w_conv1 = Variable(torch.randn(1, 32, 5))

In the forward method I replace x = F.max_pool2d(F.relu(self.conv1(x)), 2) with x = F.max_pool2d(F.relu(F.conv2d(x, self.w_conv1, padding=2),2))

And then it will give me an error:

Expected 4-dimensional input for 4-dimensional weight [1, 32, 5], but got input of size [50, 1, 28, 28] instead

The code worked before, and I thought I’d just replace the class with it’s functional equivalent. Why does this error appear ?

PS. Also posted this question on stakoverflow:


The error message is not very clear I’m afraid because it comes from deep within the C backend.
The problem here is that when you do a convolution on a 2D image with size (batch, in_chan, width, height), and you want an output of size (batch, out_chan, width’, height’), your weights for the convolution should be (out_chan, in_chan, width_kern_size, height_kern_size), basically when you use a kernel size of 5 for the Conv2d function, it is the same as having a kernel of width 5 and height 5.
Thus you should have self.w_conv1 = Variable(torch.randn(32, 1, 5, 5)). See the doc for more details.

1 Like