Nn.functional.conv2d vs nn.Conv2d?

Hello world!!
I am doing the game of life and I have the following transformation.

The question is, how can I obtain num_alive_neighbors by using nn.Conv2d instead of nn.functional.conv2d?
Thanks!!

You should instantiate nn.Conv2d for later on replacing by-default kernel with yours. However, what’s the point if you have the functional?

as @JuanFMontesinos mentioned, you can create an nn.Conv2d initialized with random weights. Then, set its parameters using your own kernel.

Something like this (you can adjust the input values of each function):

from torch.nn.parameter import Parameter
...

conv2D = nn.Conv2d(in_channels, in_channels, kernel_size, stride=stride, padding=padding,
                                dilation=dilation, groups=in_channels, bias=False)  # do not use the bias.
conv2D.weight = Parameter(data=kernel2d, requires_grad=False)  # kernel2d is your own kernel.
1 Like

Thank you very much!! That is exactly what I was looking for!!:slightly_smiling_face::slightly_smiling_face: