liveBook · Manning

Chain rule

As explained in Grokking AI Algorithms, Second Edition

The chain rule is a calculus rule for differentiating a composition of functions by multiplying the derivatives of its successive functions.

The chain rule finds how a change travels through a sequence of functions. If one value changes a second value, which changes a third, multiply the derivatives for each step to find the overall effect. It is a way to follow a mathematical domino effect. section 9.26 section 9.8

In a neural network, a weight does not change the cost directly. It changes a layer’s value, which changes the prediction, which changes the cost. Forward propagation computes the prediction; backpropagation follows those dependencies backward to find how much each weight contributed to the error. The resulting partial derivative, ∂J/∂W, describes how cost J changes with one weight W. section 9.26

Follow one weight’s effect

Consider a connection from a hidden unit to an output unit. Its weight affects the output calculation, the activation function turns that calculation into a prediction, and the prediction affects cost. Starting at the cost, work backward: find the prediction’s error, account for the activation function’s slope, then account for the hidden unit’s value. Multiplying these effects gives that weight’s cost gradient. For a layer of weights, the same calculation can be organized as a matrix operation: multiply the transpose of the hidden-layer values by the output error. section 9.26

Use the result during training:

  • Run forward propagation to obtain a prediction and compare it with the expected output. section 9.9
  • Work backward through the layers, multiplying each step’s derivative to calculate weight gradients. section 9.26
  • Adjust the weights to reduce cost. A positive ∂J/∂W calls for decreasing that weight; a negative value calls for increasing it. section 9.26 section 9.8
Figure 9.26 Formula for calculating weight updates with the Chain Rule
09_26

The weight-update diagram shows where the chain rule enters the neural-network training calculation. section 9.8

In Python, this backward calculation needs derivatives of the activation functions. For sigmoid, a learning implementation can compute the derivative as sigmoid(x) × (1 − sigmoid(x)), then alternate forward propagation and backpropagation across training epochs. The chain rule supplies the gradients; gradient descent uses them to choose weight adjustments. Other optimization approaches can also be used. section 9.9 section 9.8

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through backward output-error and weight-gradient calculations, then connects them to a Python training loop without a theoretical proof.
Examples
Uses an artificial neural network’s layer-by-layer weight updates, a sigmoid derivative, and a Python forward/backpropagation training loop.
What it assumes
Introduces composite functions and derivative notation in context while assuming familiarity with basic neural-network weights, layers, and predictions.
What you can do after
Apply it via a Python neural-network implementation to calculate weight changes from prediction error during backpropagation.
How it connects
Links composite functions and derivatives to forward propagation, backpropagation, activation functions, and gradient descent.

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 Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.