Difference of Module and Function definition of a sublayer

Hello to everyone,
I’m a newcomer to the Pytorch realm. I am faced with a question like this:

Is it necessary to define the sub-layers that we will use within the main Net module as a class outside the module, or can it be defined as a function?

Herein: https://github.com/FrancescoSaverioZuppichini/Pytorch-how-and-when-to-use-Module-Sequential-ModuleList-and-ModuleDict/blob/master/notebook.ipynb, it’s defined as function, but in https://github.com/pytorch/vision/blob/master/torchvision/models/googlenet.py, inception is created by using class…

Ex: conv - BN - ReLU

def myConv(x):
x = conv2d(x)
x = bnorm(x)
return ReLU(x)

or

class myConv(nn.Module) …

and also what is the difference?

Thanks…

Both approaches will work the same, if you are using a pure functional approach.
A custom nn.Module class is useful in case your custom module needs to store parameters, buffers etc.

E.g. nn.Conv2d itself is defined as an nn.Module, since it stores the weight, bias parameters as well as some internal attributes, e.g. padding, stride, etc.

In your current example you are just applying some modules, so you can easily define it as a function.

Thanks a lot @ptrblck !! :+1:t3: