Issue
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 withsuper().__init__()
this creates a class that tracks the architecture and provides a lot of useful methods and attributes. It is mandatory to inherit fromnn.Module
when you're creating a class for your network. The name of the class itself can be anything.
Solution
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.
Answered By - Masoud Gheisari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.