Chain Rule and Backpropagation
Objective
Apply the chain rule to scalar composite functions and simple computation graphs. Understand why branch gradients add, why batch averaging rescales gradients, and how “calculate a gradient,” “automatically record a graph,” and “update with an optimizer” are three distinct steps.
Composite functions and responsibility propagation
If and :
The effect of on equals “the effect of on the intermediate quantity × the effect of the intermediate quantity on output.”
For and :
At , the derivative is 24. Expanding verifies it.
The multivariable version also follows shape. If , , and scalar :
Backpropagation takes an upstream vector and a local Jacobian as a vector-Jacobian product, so it need not store the complete Jacobian.
Computation graph
For one linear-regression example:
Backward through the graph:
Therefore:
The gradient for is . Each local node needs only its own input and upstream gradient.
If batch loss is mean MSE:
then and . Replacing sum with mean changes gradient scale; implementation, hand calculation, and tests must use the same reduction convention.
Branches add gradients
When variable affects through two paths, the total derivative is the sum of each path’s contribution. This is why gradients accumulate for shared parameters, residual connections, and repeated use of one token embedding.
For and , one path enters the square and another adds directly:
Omitting either path produces a wrong gradient. Broadcasting, repeated indices, and shared weights can create less obvious multiple paths in code.
Reverse-mode automatic differentiation
For scalar loss and many parameters, reverse mode obtains all parameter gradients at a cost comparable to forward computation. Backpropagation is an efficient organization of the chain rule on a computation graph, not a separate mysterious formula.
Intermediate values must be saved for backward computation, contributing to training-memory use. Gradient checkpointing exchanges extra recomputation for memory. Inference normally does not retain a complete gradient graph.
Vanishing and exploding gradients
A long chain multiplies derivatives. Many factors below 1 drive a gradient toward zero; many large factors make it explode. Initialization, normalization, residual connections, activation functions, and gradient clipping all relate to this. Clipping can alleviate a numerical symptom but does not guarantee the root cause is solved.
Common misconceptions
- Forgetting to add multiple path contributions for a shared variable.
- Confusing backpropagation with parameter updating: the first calculates gradients; the optimizer updates later.
- Checking only that training runs rather than checking gradient direction.
- Changing learning rate whenever numerical and analytical gradients disagree rather than inspecting implementation.
Exercises and self-check
- Calculate derivatives for , , and in .
- Draw x → u=x² → y=u+x → L=y², list both paths from x to L, and add them.
- Derive gradients for batch-mean MSE and identify where 2/n originates.
- State Jacobian/gradient shapes for and check transpose direction.
- Explain the responsibilities of backpropagation, automatic differentiation, and an optimizer update.
- I can differentiate backward through a simple computation graph.
- I know branch gradients add.
- I can distinguish gradient calculation from parameter updates.
- I check sum/mean reduction and batch dimensions.
- I can use VJP intuition to explain why a full Jacobian is unnecessary.
References
Sources were checked on 2026-07-14. Framework automatic-differentiation behavior can change by version; recheck current official documentation before use.
- MIT OpenCourseWare: 18.02SC Partial Derivatives
- PyTorch: Autograd mechanics
- Google ML Crash Course: Backpropagation
Previous: Partial derivatives and gradients · Next: Automatic differentiation and gradient checking.