What is the chain rule?
The chain rule gives the derivative of a composed function: if y = f(g(x)), then dy/dx = f'(g(x)) · g'(x). In words, the rate of change of the whole chain is the product of the rates of change of its links, each evaluated at the value flowing through it.
Every neural network is a long composition of simple functions, so the chain rule is how its gradients are computed. Backpropagation is the chain rule applied from the loss back to every weight, and automatic differentiation in PyTorch, JAX, and TensorFlow is a general implementation of it. It also explains vanishing and exploding gradients, which shaped architectures such as ResNets and LSTMs.
Key ideas
- The derivative of f(g(x)) is f'(g(x)) · g'(x).
- The outer derivative is evaluated at the inner function's output, not at x.
- A chain of any length differentiates to a product of local derivatives.
- For vector-valued layers, the local derivatives are Jacobians and the product is matrix multiplication.
- Backpropagation is the chain rule evaluated from the output backward.
- Long products of factors below 1 make gradients vanish; factors above 1 make them explode.
Common questions
What is the multivariable chain rule?
When a variable affects the output through several paths, you add the contributions of each path. If z depends on x and y, and both depend on t, then dz/dt = (∂z/∂x)(dx/dt) + (∂z/∂y)(dy/dt). In a network, a weight that feeds several neurons gets its gradient by summing over those neurons.
How is the chain rule different from the product rule?
The product rule handles two functions multiplied together: (f·g)' = f'·g + f·g'. The chain rule handles one function fed into another: (f∘g)' = (f'∘g)·g'. Many expressions need both, for example x·sin(x²).
Why does backpropagation go backward instead of forward?
A network has one scalar loss and many parameters. Going backward computes the gradient of that one output with respect to all inputs in a single sweep. Going forward would need one sweep per parameter, so reverse mode is far cheaper whenever outputs are few and inputs are many.
Who discovered the chain rule?
Leibniz used it in the 1670s, and it appears in the early calculus texts of the following decades. Its use for training multi-layer networks was popularized by Rumelhart, Hinton, and Williams in 1986, building on earlier work on reverse-mode differentiation by Linnainmaa and Werbos.
Related explainers
- Derivatives — The calculus behind gradient descent
- Partial Derivatives & Gradients — Extend single-variable derivatives to functions of multiple variables and gradient vectors
- Matrix Calculus & Jacobians — Partial derivatives, gradients, and the Jacobian — calculus for vectors
- Backpropagation — How networks learn from their mistakes
- Vanishing & Exploding Gradients — Why deep networks struggle and how to fix it
- Skip Connections & ResNets — Residual learning and why deeper networks work
References
- Rumelhart, Hinton & Williams (1986), "Learning representations by back-propagating errors", Nature
- Baydin et al. (2018), "Automatic Differentiation in Machine Learning: a Survey"
- Goodfellow, Bengio & Courville, "Deep Learning", section 6.5 Back-Propagation
- Deisenroth, Faisal & Ong, "Mathematics for Machine Learning", ch. 5 Vector Calculus
Last updated