The documentation says "Submodules assigned in this way will be registered, " but does not actually say how this magical automatic registration happens. Could anyone explain?
Another and maybe related question for me is what kind of magic
These are basically python tricks. In Python, when you assign something as an attribute of an object, the object’s
__setattr__ method will be called. For
nn.Module, the method is overriden at https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py#L400.
super(Model, self).__init__() is not magic either. It’s the standard way to call parent class’s constructor in Python.
Thank you – so the only way of how the submodules get registered is when an attribute is directly assigned an instance of a Module subclass.
In all other cases, e.g. assigning to a component or element of an attribute or assigning something that contains Module subclass instance, this does not work and one has to call
self.add_module(...) for the submodule?