liveBook · Manning

Chain rule

As explained in Deep Learning with PyTorch, Second Edition

A differentiation rule for computing the derivative of a composite function by multiplying derivatives along the composition.

The chain rule computes how an outer result changes when an inner quantity changes. For a loss built from a model, multiply the loss derivative with respect to the model output by the model derivative with respect to each parameter. section 5.4.2

In learning, this turns a change in model parameters into a change in loss. The resulting derivatives form a gradient, which indicates how to adjust weights and bias to reduce the error. section 5.2 section 5.4.2

For a linear model followed by a mean-squared-error loss, the calculation proceeds as follows:

  • Differentiate the loss with respect to each prediction; averaging contributes a division by the number of predictions. section 5.4.2

Manual gradient for a model and loss

The implementation multiplies derivatives directly: dloss_dw = dloss_dtp * dmodel_dw(t_u, w, b) and dloss_db = dloss_dtp * dmodel_db(t_u, w, b). This is a simple backward pass through the loss and model. section 5.5

Figure 5.6 Comparing numerical vs. analytical gradient calculation. The numerical approach (arrow through points) estimates the gradient using finite differences between discrete points, while the analytical approach (arrow along the curves) calculates the exact gradient at a specific point.
figure

Compare numerical gradient estimates between discrete points with analytical derivatives evaluated at a specific point. section 5.4.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through analytical derivatives for a linear model and squared-error loss, then extends the idea to differentiable deep compositions.
Examples
Uses a linear model, mean-squared-error loss, manual derivative functions, and a combined gradient function.
What it assumes
Assumes basic model, prediction, loss, and parameter ideas while developing differentiation through a simple learning problem.
What you can do after
Apply it to compute parameter gradients manually and understand how automatic differentiation performs the same backward propagation.
How it connects
Links derivatives, gradients, loss functions, model parameters, backpropagation, and automatic differentiation.

Also explained in

Compare how 4 books explain chain rule →

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.