3

I am going through Udacity's Intro To Deep Learning with Pytorch course In the Neural Network part the instructor says that "in the init method, we need to call super, we need to do that because then PyTorch will know to register all the different layers and operation, if you don't do this part it wont be able to track the things that you are adding to your network and it wont work". Could you kindly elaborate and explain what exactly is the role of super keyword here and what does nn.Module inherits which helps in "keeping track" of changes.

Further the Jupyter notebooks says the following about use of super,

class Network(nn.Module):

Here we're inheriting from nn.Module. Combined with super().__init__() this creates a class that tracks the architecture and provides a lot of useful methods and attributes. It is mandatory to inherit from nn.Module when you're creating a class for your network. The name of the class itself can be anything.

  • please refer to the official docs https://pytorch.org/docs/stable/generated/torch.nn.Module.html – Maxim Lyuzin Jun 05 '22 at 07:23
  • you can read this: `https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods` – Salio Jun 05 '22 at 08:10

1 Answers1

2

class Network(nn.Module) means that you defined a Network class that inherits all the methods and properties from nn.Module (Network is child class and nn.Module is parent class)

By using super().__init__() , Network's __init__ will be same as its parent __init__.

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()

For example when you make a model:
model = Network()
by calling this model, actually nn.Module will handle this call and track changes.

visit this link if you want to see nn.Module source codes.

Masoud Gheisari
  • 949
  • 1
  • 9
  • 21