Create Custom Models with Object-oriented Programming

Create Custom Models with Object-oriented Programming


As long as the data is passed through the layers of a neural network in a strictly sequential order, one can always define the model using nn.Sequential, like we’ve done up until now. However, if the forward pass contains non-sequential architectures, like for example skip connections, where the output from a layer can skip the next few layers, nn.Sequential will not be sufficient. For models with non-sequential forward passes, we need to create custom model classes, which allows use to tailor the computational flow through the network to our needs.

This unit teaches you how to build custom neural network architectures using object-oriented programming and PyTorch’s nn.Module class. You will learn to define classes with attributes and methods, understand how inheritance allows one class to build on another, and apply these concepts to create flexible neural network models beyond what nn.Sequential can offer.

Tools