liveBook · Manning

Torch.nn.Module

As explained in Deep Learning with Python, Third Edition

The base class for defining stateful PyTorch models with parameters and a forward method.

torch.nn.Module packages a model’s trainable state and computation into one reusable object. Define Parameters in __init__(), implement the forward pass in forward(), and call the instance on inputs. section 3.4.2

A minimal linear model

class LinearModel(torch.nn.Module): def __init__(self): super().__init__() self.W = torch.nn.Parameter(torch.rand(input_dim, output_dim)) self.b = torch.nn.Parameter(torch.zeros(output_dim)) def forward(self, inputs): return torch.matmul(inputs, self.W) + self.b Instantiate it with model = LinearModel(), then use output = model(torch_inputs). Calling model(inputs), rather than model.forward(inputs), preserves the framework hooks added by __call__(). section 3.4.2

A Module simplifies the training loop by handing state and updates to PyTorch components:

  • Retrieve trainable Parameters with model.parameters() and pass them to torch.optim.SGD. section 3.4.2
  • Compute predictions and loss, call loss.backward(), update with optimizer.step(), and clear gradients with model.zero_grad(). section 3.4.2
  • Use Modules as building blocks within modular, hierarchical architectures and reuse them where appropriate. section 9.1
Listing 3.27 Defining a torch.nn.Module
class LinearModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.W = torch.nn.Parameter(torch.rand(input_dim, output_dim))
        self.b = torch.nn.Parameter(torch.zeros(output_dim))

    def forward(self, inputs):
        return torch.matmul(inputs, self.W) + self.b

Inspect the complete minimal Module definition, including superclass initialization, Parameter fields, and the forward method. section 3.4.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Demonstrates subclass definition, Parameter tracking, invocation, optimizer integration, training steps, and gradient clearing.
Examples
Uses a linear classifier with weight and bias Parameters, mean squared error, SGD, and a compact training step.
What it assumes
Assumes tensors, parameters, gradients, backward(), and basic PyTorch operations while introducing Module packaging.
What you can do after
Build it from scratch using torch.nn.Module, torch.nn.Parameter, and torch.optim.SGD.
How it connects
Connects Parameters, forward propagation, backpropagation, optimizers, gradient descent, and modular neural-network architectures.

Also explained in

Compare how 5 books explain torch.nn.Module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.