Math toolkit
The chain rule on a graph
When one quantity depends on another through a chain of operations, their slopes multiply. Applied on a graph, the chain rule gives every gradient in one backward pass.
Neural networks are long chains of simple operations. The chain rule says how slopes combine along such a chain: if y depends on u and u depends on x, then the slope of y with respect to x is the product of the two slopes. Applied to a whole graph, this rule gives the gradient for every input.
Concept
Before you start: a computational graph has nodes for values and edges for dependencies. The forward pass computes values from left to right. The backward pass sends gradients from right to left.
The chain rule states:
if y = f(u) and u = g(x), then dy/dx = (dy/du) · (du/dx)A three-node graph
Let a = 2, b = 1 and x = 3, and define:
u = a · x
v = u + b
y = v²The forward pass gives u = 6, v = 7 and y = 49.
The backward pass starts at ∂y/∂y = 1 and walks left:
∂y/∂v = 2v = 14 # from y = v²
∂y/∂u = ∂y/∂v · ∂v/∂u = 14 · 1 # v = u + b, so ∂v/∂u = 1
∂y/∂b = ∂y/∂v · ∂v/∂b = 14 · 1
∂y/∂a = ∂y/∂u · ∂u/∂a = 14 · x = 14 · 3 = 42
∂y/∂x = ∂y/∂u · ∂u/∂x = 14 · a = 14 · 2 = 28Each step multiplies the slope arriving from the right by the local slope. That is the whole method.
Example
Check ∂y/∂a = 42 by nudging a to 2.00001. Then u = 6.00003, v = 7.00003 and y = 49.00042. The change is 0.00042 over a step of 0.00001, which is about 42.00009. It matches.
Common mistakes
- Forgetting a factor.
∂y/∂ais∂y/∂u · x, not just∂y/∂u. The local slope ofu = a·xwith respect toaisx. - Treating addition as having a slope other than 1. For
v = u + b, both∂v/∂uand∂v/∂bare 1. - Reading the chain from the wrong end. The product starts at the output and moves back to the input.
Check yourself
- Why is
∂y/∂bequal to∂y/∂vhere? Becausev = u + b, and the slope of addition with respect to either input is 1. - If
awere 5 instead of 2, what would∂y/∂xbe?14 · a = 14 · 5 = 70.
Key takeaways
- The chain rule multiplies slopes along a path.
- On a graph, you multiply the incoming gradient by each node's local slope.
- Addition passes gradients through unchanged. Multiplication scales them by the other factor.
- Checking one derivative with a small nudge catches most algebra mistakes.
Further learning
The next question is what happens when a layer outputs a vector, not a single number: Jacobians and vector-Jacobian products.
Related
Calculus and gradients
Derivatives, partial derivatives, the chain rule and gradients. This is the math that turns a loss number into parameter updates.
Backpropagation from scratch
How training computes the gradient of the loss for every weight in one backward pass. Covers computational graphs, reverse-mode autodiff and the bugs that break training.
Computational graphs and reverse mode
A forward pass records each operation as a node in a graph. The backward pass walks that graph in reverse and applies one local rule per node, so one sweep gives every gradient.