liveBook · Manning

Chapter 6: Using a neural network to fit the data

What you will learn in chapter 6 of Deep Learning with PyTorch, Second Edition

The introduction connects the previous chapter’s training process to neural networks and explains why merely choosing a different fixed-form model is not enough for more complex problems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction connects the previous chapter’s training process to neural networks and explains why merely choosing a different fixed-form model is not enough for more complex problems.

Key terms: backpropagating errors, parameters, gradient, loss, quadratic model, differentiable, function’s shape

6.1 Artificial neurons

Introduces neural networks as compositions of simple functions and defines a neuron as a linear transformation followed by a nonlinear activation.

Key terms: neural networks, composition, neuron, weight, bias, activation function, tanh, layer of neurons, vector-valued, multidimensional weights and biases

6.1.1 Composing a multilayer network

Explains how a multilayer neural network composes layers and uses matrix–vector calculations to represent a whole layer of neurons.

Key terms: multilayer neural network, layer of neurons, matrix, vector, linear algebra

6.1.2 Understanding the error function

This section contrasts the error surface of the earlier linear model with that of neural networks, explaining why network parameters must be understood collectively and why nonlinearity helps model real-world data.

Key terms: linear model, error-squared loss function, convex error curve, nonconvex, parameters, useful output, individual interpretability, collective system, activation functions, nonlinearity

6.1.3 Adding nonlinearity with activation functions

This section explains why neural networks place nonlinear activation functions after linear operations, both to model varied relationships and to produce useful output ranges.

Key terms: linear operation, nonlinear activation function, different slopes, last layer, output range, torch.nn.Hardtanh, torch.nn.Sigmoid, torch.tanh, sensitive range, boundary values

6.1.4 More activation functions

The section compares several activation functions and explains when ReLU, Sigmoid, and LeakyReLU are useful.

Key terms: Tanh, Softplus, Hardtanh, ReLU, rectified linear unit, Sigmoid, logistic function, 0…1 range, LeakyReLU, small positive slope

6.1.5 Choosing the best activation function

Explains how an activation function’s nonlinearity and sensitive ranges let networks fit complex functions while determining which units receive useful learning signals.

Key terms: activation functions, nonlinear, differentiable, affine linear, sensitive range, insensitive, saturated, gradient, backpropagation, linear + activation units

6.1.6 What learning means for a neural network

This section explains how composed neurons can represent nonlinear relationships and how learning tunes their parameters from examples rather than relying on a hand-designed function.

Key terms: linear transformations, differentiable activations, Tanh, neurons, layers, training, weights, biases, loss function, backpropagate

6.2 The PyTorch nn module

Introduces torch.nn modules, how they hold trainable parameters and submodules, and nn.Linear as the built-in counterpart to the earlier linear model.

Key terms: torch.nn, module, nn.Module, nn.Parameter, submodules, nn.ModuleList, nn.ModuleDict, optimizer, nn.Linear, affine transformation

6.2.1 Using nn.Module as a callable

Explains how calling an nn.Module runs its forward computation and why user code should not call forward directly.

Key terms: nn.Module, __call__, forward, hooks

6.2.2 Returning to the linear model

The section replaces the hand-written linear model with nn.Linear, reshapes inputs for batching, and uses module parameters and a built-in loss in the existing training loop.

Key terms: nn.Linear, input features, output features, bias, weight, batch, zeroth dimension, B × N_in, unsqueeze, batching

6.3 Finally, a neural network

This section explains why the book moves from a linear model to a neural network using the familiar calibration problem, despite expecting no improvement in model quality.

Key terms: linear model, neural network, approximating function, calibration problem

6.3.1 Replacing the linear model

The section replaces a one-feature linear model with a small neural network and shows how nn.Sequential passes data through its layers.

Key terms: hidden layer, hidden features, tanh activation, linear layer, capacity, nn.Sequential, modules, intermediate outputs, loss function

6.3.2 Inspecting the parameters

This section shows how to inspect a sequential neural network’s trainable parameters and gradients, including how to find parameters by name or through submodules.

Key terms: model.parameters(), grad, loss_train .backward(), optimizer.step(), nn.Tanh, activation functions, parameters, named_parameters, Sequential, OrderedDict

6.3.3 Comparing to the linear model

This section compares the neural network’s fitted output with a line and uses the plot to illustrate overfitting.

Key terms: overfit, parameters, measurements

6.4 Conclusion

The section recaps how chapters 5 and 6 built and trained differentiable models, moving from raw autograd to PyTorch’s nn module.

Key terms: differentiable models, gradient descent, autograd, nn

6.5 Exercises

6.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: The mechanics of learning Chapter 7: Telling birds from airplanes: Learning from images →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.