liveBook · Manning

Chapter 5: The mechanics of learning

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

The introduction describes learning in terms of paired examples and the ability to produce outputs for sufficiently similar new inputs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction describes learning in terms of paired examples and the ability to produce outputs for sufficiently similar new inputs.

Key terms: input data, desired outputs, new data, deep learning

5.1 A timeless lesson in modeling

Kepler’s modeling process illustrates how fitting and validating a parameterized model from data relates to learning, and why the book focuses on adaptable general models.

Key terms: model, ellipse, parameters, validation, independent observations, fit the data, learn from data, unknown parameters, general models, neural network

5.2 Learning is just parameter estimation

The section sketches how learning estimates model weights by comparing predictions with desired outputs and repeatedly reducing error.

Key terms: ground truth, weights, forward pass, loss, gradient, backward pass

5.2.1 A hot problem

The section introduces an unmarked thermometer as an example of a regression problem: fitting model outputs to continuous temperature values.

Key terms: regression problem, continuous values

5.2.2 Gathering some data

The section gathers paired Celsius and new-thermometer readings for estimating what the thermometer’s unknown units mean, while noting that the readings may be noisy.

Key terms: t_c, t_u, noise

5.2.3 Visualizing the data

A quick plot shows noisy data with an apparent pattern, while noting that the example was fabricated to follow a linear model.

Key terms: noisy, pattern, linear model

5.2.4 Choosing a linear model as a first try

Introduces a linear model for converting unknown temperature readings to Celsius and frames fitting it as minimizing prediction error.

Key terms: linear model, weight, bias, t_u, parameters, fitting, loss function, optimization, w, b

5.3 Less loss is what we want

The section defines loss as a numerical measure to minimize and explains why squared error is chosen over absolute error for the temperature model.

Key terms: loss function, cost function, t_p, t_c, absolute difference, squared difference, t_p - t_c, smooth, well-defined minimum

5.3.1 From problem back to PyTorch

This section expresses the linear model and mean squared loss in PyTorch, then explains the broadcasting that lets scalar parameters operate on tensor inputs.

Key terms: t_u, w, b, zero-dimensional tensors, mean square loss, broadcasting, element wise scaling, element wise offset, index dimension

5.4 Down along the gradient

This section uses a two-knob analogy to explain how gradient descent lowers loss by adjusting parameters and taking smaller steps near a minimum.

Key terms: gradient descent, parameters, loss, w, b, minimum

5.4.1 Decreasing loss

This section turns the idea of adjusting model parameters to reduce loss into a numerical gradient-descent update.

Key terms: rate of change, slope, delta, central difference approximation, gradient descent, learning_rate, parameters, loss

5.4.2 Getting analytical

This section replaces finite-difference estimates with analytical derivatives and combines them into a gradient for the linear model’s weight and bias.

Key terms: delta, neighborhood, loss function, derivative, chain rule, gradient, sum of squares, t_p.size(0), torch.stack, w

5.4.3 Iterating to fit the model

This section explains how repeated parameter updates form a training loop and how the learning rate affects whether that loop converges or diverges.

Key terms: training loop, epoch, iteration, batch, learning_rate, grad, diverges, converging, hyperparameter

5.4.4 Normalizing inputs

This section rescales temperature inputs to make gradient-descent updates stable, then interprets the fitted parameters and remaining loss.

Key terms: normalization, gradient, weight, bias, learning rate, t_u, loss, gradient descent, converge, epochs

5.4.5 Visualizing (again)

The section uses a plot to check the fitted linear model against the data and explains the PyTorch tensor argument unpacking used in the plotting code.

Key terms: visualizing our data, linear model, measurements, argument unpacking, leading dimension, params

5.5 PyTorch’s autograd: Backpropagating all things

The section explains how backpropagation obtains loss gradients through differentiable functions and why writing those derivatives by hand becomes cumbersome for deep models.

Key terms: backpropagation, gradient, chain rule, parameters, loss, analytical expression, derivatives, differentiable, linear and nonlinear functions

5.5.1 Computing the gradient automatically

This section shows how PyTorch autograd computes gradients for a model and how to manage those gradients during parameter updates.

Key terms: autograd, requires_grad, grad, computation graph, backward, leaf nodes, zero_, accumulate, torch.no_grad(), in-place updates

5.5.2 Optimizers à la carte

This section shows how PyTorch optimizers update model parameters in a training loop and how to switch optimization strategies without rewriting that loop.

Key terms: optimizer, parameters, grad attribute, zero_grad, backward, step, gradients, SGD, stochastic gradient descent, Adam

5.5.3 Training, validation, and overfitting

This section explains how independent validation data reveal overfitting, how training and validation losses diagnose model behavior, and how to split data and respond to poor generalization.

Key terms: validation set, training set, overfitting, generalizing, training loss, validation loss, underfitting, capacity, regularization, L1

5.5.4 Training set

Explains why a test set must remain independent for final evaluation and how data leakage can make performance estimates misleading.

Key terms: training set, validation set, test set, hyperparameters, overfit, data leakage

5.5.5 Autograd nits and switching it off

This section explains why validation does not affect training gradients and how to avoid unnecessary autograd work during validation.

Key terms: computation graph, train_loss, val_loss, backward, parameters, zero_grad, gradients, torch.no_grad, set_grad_enabled, autograd graph

5.6 Conclusion

The conclusion explains why the chapter used a simple model to make learning through model optimization easier to understand.

Key terms: simple model, thermometer problem, neural network

5.7 Exercises

5.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Real-world data representation using tensors Chapter 6: Using a neural network to fit the data →

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.