1. How expensive is a gradient?
In Lesson 12, every training step used derivatives of the loss with respect to weights and biases. We wrote those derivatives explicitly for one neuron. How can we calculate them for a network with many layers and parameters?
One approach is numerical differentiation. Perturb a parameter slightly and measure the change in loss:
The vector selects the parameter to perturb. After calculating the baseline loss, this forward-difference approximation needs an additional forward evaluation for each parameter. With parameters, that means additional evaluations.
A central difference uses perturbations on both sides and often provides a better numerical approximation, but needs two evaluations per parameter. Choosing the perturbation size also involves a trade-off between approximation error and floating-point cancellation.
Numerical derivatives are useful for checking analytic gradients on small examples. Repeating a whole network evaluation for every parameter is an expensive way to train a large model.
2. The chain rule carries gradients backwards
For a scalar composition , the chain rule gives:
Read it as: how the loss changes with an intermediate value, multiplied by how that intermediate value changes with its input.
In a neural network, each operation receives a gradient from later operations, combines it with its local derivative, and sends the resulting gradient to earlier operations.
The forward pass computes intermediate values needed by the derivative calculations. The backward pass reuses them. If a value affects the loss through multiple branches, its gradient is the sum of contributions from all those branches, not just one path.
For vector operations, the same principle uses Jacobians and vector products. Reverse-mode differentiation propagates a loss gradient without needing to construct a full Jacobian explicitly.
A gradient measures local sensitivity, not moral “responsibility” or a percentage share of an error. Its magnitude and sign tell us how a small parameter change affects the chosen loss at the current values.
3. Follow the numbers through one network
Use a deliberately simple scalar graph:
The target is six. During the forward pass:
Work backwards, using those saved values:
Multiply the local derivatives:
With learning rate , update the weight:
A new forward evaluation gives:
The loss has fallen from nine. We used one forward pass to obtain the values and one backward pass to obtain the gradient. Evaluating the updated loss is a new forward pass, after the update.
The fruit neuron closes the loop
For a binary classifier:
Differentiate each local operation:
Their product simplifies to:
Fixed weight 4, fixed bias −2. This experiment calculates gradients without updating parameters.
Forward score:
Apple probability:
Loss:
Loss derivative with respect to probability:
Sigmoid derivative:
Score gradient:
Weight gradient:
Bias gradient:
This is precisely the per-example derivative used in Lesson 12. Averaging it across the eight examples gives that experiment's full-batch gradient.
4. A complete case: gradients through a hidden layer
A single neuron has only one affine layer. To see gradients pass through a hidden layer and split across branches, use a small graph:
The inputs are colour depth and size. The example starts at , , with watermelon as the true class. These are illustrative teaching features, not reliable real-world fruit identification rules.
The network has four weights and no biases:
Follow the experiment's six stages in order. You can change features, labels and weights; the values and gradients recalculate immediately.
Adjust the four weights manually
1. Network structure
Two input features feed one Sigmoid hidden unit, which feeds two class scores. There are four weights and no biases. This is a computation demonstration for one selected example.
Editing an input, label or weight starts a new loss history at the current weights. All updates train only the selected example.
Trace the backward pass from A to E
A. Output scores. For Softmax and one-hot cross-entropy, let be the target entry for class :
B. Hidden output. The hidden value feeds both class scores, so add both gradient contributions:
C. Local Sigmoid derivative.
D. Hidden score. Combine the incoming gradient and local derivative:
E. All four weights.
Calculate all these derivatives using the same current weights, then update the parameters together. Updating an output weight before computing the hidden gradient would mix two parameter states and would no longer be the gradient of the original forward pass.
What this demonstration cannot establish
The training simulation repeatedly optimises one selected example, up to 100 updates. A falling loss on that example does not establish classification ability on other fruit.
There is also a structural limitation: without output biases, . Since a Sigmoid hidden value is positive, the sign of this difference depends only on . At fixed weights, this graph therefore picks the same highest-scoring class for every input, unless the two scores tie.
We preserve this small graph to make the chain rule transparent. A useful classifier would need a more suitable architecture, labelled data from both classes, and evaluation on held-out examples. Backpropagation computes the gradients of the graph we give it; it does not remove the graph's limitations.
5. Vanishing and exploding gradients
Gradients through a sequence of operations involve products of local derivatives. Repeated contraction can make gradients very small; repeated amplification can make them very large.
Sigmoid has derivative at most . Multiplying 100 such factors gives:
This illustrates severe attenuation. A real network's gradient also contains weight matrices, sums over paths and other operations, so this is not a universal formula for its complete gradient. A very small gradient can slow learning in early layers; it need not be literally zero.
Some tools that help address these difficulties include:
- ReLU: derivative one on its positive branch, avoiding Sigmoid's contraction there. Its inactive branch has derivative zero, and the rest of the network can still attenuate or amplify gradients.
- Residual connections: add an identity path through blocks, helping gradients pass through deep networks. We will study them later in the course.
- Initialisation and normalisation: influence activation and gradient scales.
- Gradient clipping: limits large gradients, addressing explosion rather than restoring vanishing gradients.
Automatic differentiation does the bookkeeping
Modern machine-learning frameworks can record operations and compute derivatives automatically. Backpropagation is the familiar reverse-mode application used to obtain parameter gradients for a scalar training loss.
You usually build the computation and ask the framework for gradients rather than manually writing every derivative. The optimiser then performs a separate update. Understanding the chain rule still helps you diagnose incorrect graphs, missing gradient paths and unstable training.
The cost of a backward pass is usually comparable in order to the forward computation, rather than growing by one whole forward evaluation per parameter. It is not always exactly twice the forward cost. Retaining intermediate values also has a memory cost; techniques that recompute some values trade computation for memory.
6. The neural networks chapter is complete
The seven lessons in Chapter II now connect the pieces:
- Lesson 7: weighted sums, biases and neurons.
- Lesson 8: nonlinear activation functions.
- Lesson 9: layers, parameters and the training loop.
- Lesson 10: losses that define a numerical objective.
- Lesson 11: Softmax probabilities and temperature.
- Lesson 12: learning rates, mini-batches and optimisers.
- Lesson 13: the chain rule, computation graphs and efficient gradients.
Backpropagation computes gradients; the optimiser uses them to update parameters. Forward values are reused, local derivatives are multiplied along paths, and contributions are added at branches.
Next we move into natural language processing: can counting short word sequences help us predict the next word?