liveBook · Manning

Torch.nn.Module

As explained in Math and Architectures of Deep Learning

The PyTorch base class that neural network models subclass to define learnable modules and a forward pass.

torch.nn.Module is the base class for defining PyTorch neural network models. A model subclasses it, creates its layers in __init__(), and defines how inputs move through those layers in forward(). Calling the model, such as nn(X), invokes that forward pass. section 8.5 section 10.6.1

A Module commonly combines:

  • Learnable layers such as Linear or Conv2d, instantiated during initialization. section 8.5 section 10.6.1
  • Nonlinear operations such as Sigmoid, arranged with other layers in torch.nn.Sequential. section 8.5
  • A forward computation that passes input tensors through the assembled architecture and returns predictions or intermediate outputs. section 8.5 section 10.6.1

Two-layer regression network

A demand-prediction model subclasses torch.nn.Module and stores a Sequential pipeline: Linear(input_size, 10), Sigmoid, Linear(10, 5), Sigmoid, and Linear(5, 1). Its forward() method returns self.model(X). The resulting module can be called during training, passed to MSELoss, and supplied to an SGD optimizer through nn.parameters(). section 8.5

class TwoLayeredNN(torch.nn.Module):
    def __init__(self, input_size, hidden1_size, hidden2_size, output_size):
        super(TwoLayeredNN, self).__init__()

        self.model = torch.nn.Sequential(                  #1

            torch.nn.Linear(input_size, hidden1_size),     #2
            torch.nn.Sigmoid(),

            torch.nn.Linear(hidden1_size, hidden2_size),   #3
            torch.nn.Sigmoid(),

            torch.nn.Linear(hidden2_size, output_size)     #4
        )

    def forward(self, X):                                  #5
        return self.model(X)

nn = TwoLayeredNN(input_size=X.shape[-1], hidden1_size=10,
                                  hidden2_size=5, output_size=1)

Inspect the complete Module definition showing initialization, Sequential composition, layer sizes, forward(), and model construction. section 8.5

How Math and Architectures of Deep Learning covers it

How deep it goes
Descends through subclassing, initialization, Sequential chaining, forward execution, and optimizer integration, but not Module internals.
Examples
Uses a demand-prediction multilayer perceptron and a convolutional network with three Conv2d layers.
What it assumes
Assumes familiarity with input tensors, neural-network layers, loss functions, and gradient-based training while demonstrating the model structure.
What you can do after
Build it from scratch by subclassing Module, composing layers, implementing forward(), and training through parameters and an optimizer.
How it connects
Connects forward pass, Sequential, Linear, Sigmoid, Conv2d, automatic differentiation, MSE loss, and SGD optimization.

Also explained in

Compare how 6 books explain torch.nn.Module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.