Custom layers
In the previous section, we briefly mentioned the nn.Module
class as a base parent for all NN building blocks exposed by PyTorch. It's not only a unifying parent for the existing layers—it's much more than that. By subclassing the nn.Module
class, you can create your own building blocks which can be stacked together, reused later, and integrated into the PyTorch framework flawlessly.
At its core, nn.Module
provides quite rich functionality to its children:
- It tracks all submodules that the current module includes. For example, your building block can have two feed-forward layers used somehow to perform the block's transformation.
- It provides functions to deal with all parameters of the registered submodules. You can obtain a full list of the module's parameters (
parameters()
method), zero its gradients (zero_grads()
method), move to CPU or GPU (to(device)
method), serialize and deserialize the module (state_dict()
andload_state_dict()
), and even...