What's the nn.Module magic?

Hi, let’s see a typical case:

class MyLayer(nn.Module):
  def __init__(self)
      super(...)

     self.a = nn.Parameters(...)
     self.b = nn.Linear()

I wonder how can nn.Module :

  1. add self.a into self.named_parameters() and
  2. self.b to self.named_children and self.named_module.

Could anyone introduce the magic behind nn.Module.
Everytime I use nn.Module, I felt worried since I dnt understand what nn.Module DID for me.

Basically if you assign some member in nn.Module the internal __setattr__ method is called.
Have a look at the source code.
Based on the type you try to assign, the conditions call the appropriate method to register the parameter or buffer etc.

2 Likes