liveBook · Manning

Gradient descent

As explained in Deep Learning with PyTorch, Second Edition

Gradient descent is an iterative optimization method that updates parameters opposite an objective function’s gradient, scaled by a step size, to reduce the objective.

How does a model learn the right settings? Imagine two knobs, weight w and bias b, and a display showing how wrong the model’s predictions are. That displayed value is the loss. Gradient descent repeatedly adjusts the knobs in directions that reduce it. The gradient gives the loss’s rate of change for each parameter; the update moves against that gradient. section 5.4 section 5.4.1

For a model with parameters w and b, one update works like this:

  • Compute predictions and their loss against the desired outputs. section 5.4.1
  • Find how the loss changes with each parameter. One numerical approach tests w + delta and w - delta, then divides the difference between their losses by 2 × delta. section 5.4.1
  • Update each parameter: w = w - learning_rate × loss_rate_of_change_w, and likewise for b. The learning rate keeps the adjustment small because a slope measured nearby may not hold farther away. section 5.4.1
  • Repeat. Watch whether loss falls; an overly large adjustment can instead raise it. section 5.4 section 5.4.4

Fitting thermometer readings

Suppose a linear model predicts Celsius readings as C = w × unknown_reading + b. Scale the input readings by 0.1, start with w = 1 and b = 0, and run 5,000 updates at a learning rate of 0.01. The reported loss falls from 80.36 to about 2.93, with learned parameters near w = 5.367 and b = -17.301. Because the input was scaled, the effective weight on the original readings is about 0.537. The readings turn out to be Fahrenheit; the fitted values are close to the Fahrenheit-to-Celsius conversion. A nonzero final loss can reflect noisy measurements rather than a failed update. section 5.4.4

PyTorch can calculate gradients instead of estimating every slope by changing parameters manually. Pass model.parameters() to optim.SGD, compute a loss such as nn.MSELoss(), then call optimizer.zero_grad(), loss.backward(), and optimizer.step() inside the training loop. backward() accumulates gradients on the parameters; step() uses them to change those parameters. Clearing gradients before the next backward pass prevents unwanted accumulation. section 6.2.2

The same pattern extends beyond one weight and bias. A network built from linear layers and differentiable activations can fit nonlinear relationships; gradient descent updates the layers’ trainable parameters. An activation such as Tanh transforms values but has no parameters of its own to update. section 6.1.6 section 6.3.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Derives updates from numerical slopes, then traces automatic gradients and optimizer steps through a PyTorch training loop.
Examples
Works through thermometer-reading conversion and PyTorch linear and multilayer models with printed losses and parameters.
What it assumes
Introduces loss reduction through a knob analogy and explains numerical slopes before using differentiable PyTorch models.
What you can do after
Build it from scratch with parameter updates, then apply it via PyTorch autograd and optim.SGD.
How it connects
Connects loss functions and learning rates to autograd, stochastic gradient descent, input normalization, and neural network parameters.

Also explained in

Compare how 18 books explain gradient descent →

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.