Why does a model definition have both a __init__ and forward function?

Could someone explain to me why pytorch has both init and forward function in a model definition, and what each one does, and why do we need 2 function rather than just one?

This tutorial might be a good starter to understand why nn.Modules are built in this way.
The TL;DR: all parameters and submodules are defined and registered in the __init__ and used in the forward method.

1 Like