Why use nn.Conv2D over nn.functional.conv2d?

Hi, I stumbled upon this question because I am trying to have control over how my convolutional weights are initialized.

At any rate, we can create a 2D convolutional layer via nn.functional.conv2d, or, via nn.Conv2d

The API for both of those however seems different.

For the former:

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

For the latter:

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

So, my questions are the following:

  1. Why should I use one over the other?
  2. I am trying to initialize my convolutional weights with what I want, so is that possible using nn.Conv2D? I guess one way is to copy over the proper weights that I want, so is that basically it?


  1. It is recommended to use nn.Conv2D because it uses the nn.Module abstraction and nicely ties into the torch.optim framework well.

  2. Yes, here’s an example of initializing the weights of a ConvNet via a custom weight initialization: