liveBook · Manning

Torch.nn

As explained in Deep Learning with PyTorch, Second Edition

PyTorch’s neural-network module namespace containing classes for layers, containers, and other building blocks.

When a linear model is too limited, torch.nn provides the building blocks for a neural network. Its layers and other components let you assemble a model; training still needs data and an optimizer. section 1.5 section 6.0

These building blocks are called modules. A module derives from nn.Module and can hold nn.Parameter tensors whose values are optimized during training. It can also hold submodules and track their parameters. section 6.2

A few useful pieces of the namespace are:

  • nn.Linear applies an affine transformation using weight and bias parameters—like a simple linear model. section 6.2
  • nn.Conv1d, nn.Conv2d, and nn.Conv3d provide convolutions for time series, images, and volumes or videos, respectively. section 8.2
  • Keep submodules as module attributes so their parameters can be found. For a list or dictionary of submodules, use nn.ModuleList or nn.ModuleDict. section 6.2

A convolution for an RGB image

Use `conv = nn.Conv2d(3, 16, kernel_size=3)` for three input channels, 16 output channels, and a 3 × 3 kernel. Its weight shape is `[16, 3, 3, 3]`, and its bias shape is `[16]`. section 8.2

# In[9]:
img, _ = cifar2[0]
output = conv(img.unsqueeze(0))
img.unsqueeze(0).shape, output.shape

# Out[9]:
(torch.Size([1, 3, 32, 32]), torch.Size([1, 16, 30, 30]))

The listing shows a single image gaining a batch dimension before convolution: input shape `[1, 3, 32, 32]` becomes output shape `[1, 16, 30, 30]`. section 8.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines modules, parameters, and submodule tracking, then works through linear and convolutional layers rather than cataloging the whole namespace.
Examples
Uses a temperature-conversion linear model and a bird-versus-airplane image convolution with concrete tensor shapes.
What it assumes
Builds on a simple linear model and training loop, while defining neural network modules before using them.
What you can do after
Apply it via PyTorch nn.Linear and nn.Conv2d, while keeping submodules visible for parameter tracking.
How it connects
Connects neural network modules to trainable parameters, optimizers, linear models, and convolutions.

Related concepts

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