0.3 — Chain rule and computational graphs
Goal: Write the multivariable chain rule as a sum over paths in a DAG and compute ∂f/∂x for f(x) = sin(x²)·exp(x) by hand in reverse mode.
1. Why this session exists
All of backpropagation is one thing: the chain rule applied to a directed acyclic graph (DAG). If you leave this session knowing how to write the chain rule as a sum over paths in the graph, all of Chapter 2 (MLPs and backprop) becomes bookkeeping rather than mystery.
You already know the scalar version from Calc I. What we'll do here is lift it first to several intermediate variables, then to general DAGs, and finally walk through the canonical example pen-in-hand.
2. Refresher: scalar chain rule
If and we set , then and
Conceptually, a small perturbation produces , which in turn produces . In the limit, the ratio is the product of the two local derivatives.
Warm-up example: , set , , so
Nothing new. But notice the structure: we multiply the local derivative at every link in the chain. This structure is the only thing that survives the generalisation.
3. Multivariable with one scalar input
Suppose now that depends on intermediates , and that each depends on :
A perturbation now propagates through parallel paths — one per — and the effects add linearly to first order:
Dividing by and taking the limit,
This is the multivariable chain rule with one input. Note its shape: a sum over links, each link carrying a product of local derivatives. This is more than coincidence; it's the special case of a much more general identity, coming next.
4. DAGs and the sum over paths
In any deep learning model, the output (say, the loss) is computed from an input through a potentially complicated sequence of intermediate operations. We represent this computation as a directed acyclic graph: each node is a variable (scalar, vector, or tensor), and each directed edge indicates that is a direct function of (and possibly of other variables).
The question is: how does depend on overall? In the scalar case the answer is the sum over paths:
In words: to find how responds to , walk every directed path in the DAG from to ; along each path, multiply the local partials along its edges; sum the results.
The proof is an easy induction on graph depth. For a node fed directly by ,
and expanding each recursively until reaching , each term in the final expansion corresponds to one path . Applying this with gives the identity.
5. The computational graph as a data structure
In practice, a deep learning framework builds the DAG in two phases:
- Forward pass. Evaluate each node in topological order from input to output , storing the intermediate values (because they appear in the local derivatives).
- Backward pass. Walk the graph in reverse topological order, accumulating at each node by summing contributions from its children. When it reaches , you have ready.
Each edge carries one local partial (scalar, vector, or matrix depending on the node types); each node is responsible for summing the contributions arriving from its children. That's it.
The advantage of the graph framing over memorising chain-rule formulas is scalability: the same algorithm handles linear chains, branches, joins (multiple paths meeting at the same node), and any DAG, with no special cases.
6. Worked example:
Computing two ways: (a) by the direct scalar chain rule, as a check, and (b) by the sum over paths in the DAG, to internalise the method that will scale to deep networks.
6.1 Identifying intermediate nodes
Decompose the function into elementary operations:
appears in two places in the original formula — once inside and once in . This will become a branch in the DAG.
6.2 The DAG
6.3 Local derivatives (one per edge)
For each edge of the DAG, compute alone, treating the other parents of as constants:
Five local derivatives, one per edge. Each is trivial in isolation — that's why the method scales.
6.4 The paths from to
Inspecting the DAG, there are exactly two directed paths:
6.5 Contribution of each path
The rule: multiply the local derivatives along the edges of the path.
Path 1:
Path 2:
6.6 Final sum
6.7 Sanity check via direct scalar chain rule
To validate, differentiate by the product rule and the classical chain rule. With and :
Identical. The sum-over-paths is not a different technique — it is the same chain rule, reorganised to make the combinatorial structure explicit when a variable shows up in multiple places.
7. Why this is already (almost) backprop
Look again at §6.5. To compute Path 1's contribution, we walked the edges in the direction (forward mode). But notice that we could equally have walked in the direction (reverse mode) and obtained the same product — multiplication is commutative. What changes is the order in which partial products are accumulated.
In reverse mode, we start with at the terminal node and walk backwards, multiplying by each edge's local partial and accumulating the sum at nodes where paths meet. The result at is .
The advantage of reverse mode when there are many inputs and one output (the case for every ML loss function): a single backward pass computes for every parameter simultaneously. Cost is proportional to the number of edges, not to the number of parameters. This is the asymptotic win that makes training networks with billions of parameters tractable.
We'll formalise the algorithm in Chapter 2. For now it's enough to know that backprop is sum-over-paths, computed from output to input.
Let .
- Identify the intermediate nodes (hint: , , , ) and draw the DAG.
- List every edge and its local derivative.
- How many directed paths exist from to ? From to ?
- Use the sum over paths to compute and . Verify by direct calculation with the product rule.