liveBook · Manning

Torch.nn.Module

As explained in Inside Deep Learning: Math, Algorithms, Models

torch.nn.Module is PyTorch’s base class for neural-network components, providing a structure for computation and managing registered parameters and submodules.

A PyTorch Module organizes a neural-network component: its forward function maps inputs to outputs, while PyTorch handles backward computation unless you intervene. Standard modules live in torch.nn; custom modules can be built from tensors, Parameter, and torch.nn.functional objects, and can contain other modules. section 2.1.5

From layer to model

For d inputs and C outputs, nn.Linear(d, C) provides a linear layer with a bias. A larger model can compose layers, as in a Sequential classifier with Linear and Tanh layers, then pass the model to a training function. section 2.1.5 section 2.4.4

Figure 2.3 The Model box denotes the neural network fΘ(∙) that we want to train. This could be a small network or a very complex one. It is encapsulated into one model object with a single set of parameters Θ. The same process works for almost any network definition.

Shows a neural network encapsulated as one model object with a single set of parameters. section 2.1.5

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Defines forward and automatic backward behavior and describes module composition, but does not detail custom-module implementation.
Examples
Uses a linear layer and a Sequential moons classifier built from Linear and Tanh layers.
What you can do after
Apply it via PyTorch by composing layers into a model and passing that model to a training function.
How it connects
Connects to nn.Linear, Parameter, torch.nn.functional, model.parameters(), and model.train()/eval().

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 Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.