Chain Rule

The chain rule tells how derivatives pass through a composition of functions. It is the basic mathematical rule behind backpropagation.

For scalar functions:

the derivative is:

The important idea is that a change in first changes , and the change in then changes .

Computational Graph View

In a computational graph, each node stores a local operation and the backward pass multiplies local derivatives along paths.

For:

the gradient is:

This is why local derivatives are enough for global gradients.

Neural Network View

For a model layer:

and a scalar loss , backpropagation repeatedly applies:

and:

This is why deep learning frameworks can compute gradients by traversing a computational graph backward from the loss.

Vector Form

For vector-valued functions:

the Jacobian chain rule is:

Shape checking is the safest way to read this equation. If , , and , then:

Vector-Jacobian Product

Deep learning frameworks rarely materialize full Jacobians for every layer. They propagate vector-Jacobian products.

If , then:

This is efficient when the final loss is scalar. It also explains why backpropagation is usually cheaper than building every Jacobian explicitly.

Multiple Paths

If a variable affects the loss through multiple paths, gradients add:

In practice this appears in residual connections, shared parameters, branching networks, and recurrent unrolling.

For example, with a residual block:

the derivative is:

The identity path helps gradient flow.

Breaks in the Chain

Some operations block or alter gradient flow.

OperationEffect
detach / stop-gradienttreats a value as constant
hard argmax or discrete samplenot differentiable without estimator or relaxation
maskingzeroes selected gradient paths
clippingchanges gradient magnitude
numerical instabilitycan create NaN or exploding gradients

Why It Matters

  • Backpropagation is repeated chain rule.
  • Normalizing flows need Jacobian terms from invertible transformations.
  • Coordinate refinement and force prediction need gradients through geometry.
  • Recurrent models and diffusion samplers apply chain-like updates across time steps.
  • Detach or stop-gradient operations intentionally break part of the chain.

Checks

  • What is the outer function and what is the inner function?
  • Is the output scalar or vector-valued?
  • Are the derivative shapes compatible?
  • Does the computation include a detach, discrete sample, mask, or non-differentiable operation?
  • Is the chain rule being used for training, sensitivity analysis, or a change of variables?
  • Are multiple gradient paths being summed correctly?
  • Is the framework computing a vector-Jacobian product rather than a full Jacobian?