Computation Graphs and the Chain Rule
Any calculation can be drawn as a graph of tiny operations, and each tiny operation has an easy derivative. The derivative of the output with respect to any input is those little derivatives multiplied along each path, then added up over all the paths.
What the symbols mean
- Output
The final result of the calculation, in machine learning the loss.
- Intermediate value
Something computed on the way from x to L.
- Input
The value we want the sensitivity to.
What the symbols mean
- Local derivative
How one operation's output v reacts to one of its inputs u, everything else held fixed.
Explanation
A computation graph is simply a recipe for a calculation drawn as a picture: every small operation (an add, a multiply, a square) is a node, and arrows show which results feed into which. It's a graph in the discrete-math sense, dots joined by arrows (see Graphs). Here's why it's worth drawing. The derivative of a big formula is hard to see all at once, but the derivative of one tiny operation is easy: the derivative of with respect to is , and the derivative of with respect to is . The second formula above looks heavy, but it says only two things: multiply the little derivatives along a path, and add up over all the paths from to . That's the chain rule you already know, nothing new; bear with the notation.
Breaking a formula into pieces. Take . A computer doesn't do that in one go. It computes first, then . Draw and feeding a "+" node that outputs , then and feeding a "×" node that outputs . Running the recipe from left to right is the forward pass. With , , : and .
Each edge gets a local derivative. Every arrow carries the derivative of the node it points into with respect to the node it comes from. It's called local because it only looks at one small operation. For the "+" node, and . For the "×" node, and . Notice the pattern: a multiply hands each input the other input's value.
Multiply along a path. How does react to ? only reaches through . Nudge by a little bit : then moves by , and moves by times that. So . That's exactly the Chain Rule. You can check it with real numbers: gives and , a change of , which is times the nudge of .
Add over paths. Sometimes one value reaches the output by two routes. Take with and . Nudging moves and , and each one moves . For small nudges the two effects simply add, because (the tangent-plane approximation). So
That's the Multivariable Chain Rule. Look closely and you'll see it's also the product rule: . The product rule is just the chain rule on a graph with two paths.
Backward is smarter than listing paths. In a network, paths multiply fast. With one input, a layer of 3 neurons and a layer of 4, there are already paths to the output; with ten layers of 100 neurons there are . Nobody lists them. Instead, start at the output with and walk the graph backward. Each node, once it knows the derivative of the output with respect to itself, passes back to each of its inputs (its own derivative) × (the local derivative on that edge). A node that feeds several others adds up everything that comes back. Every path still gets counted exactly once, but the shared parts of paths are multiplied only once. That's backpropagation. The forward pass visits nodes in topological order (every node after the nodes it needs, as in Networks as Graphs and Partial Orders); the backward pass visits them in reverse.
Two node types are worth remembering because they show up everywhere. An add node passes the gradient it receives straight through to every input, unchanged. A multiply node sends each input the gradient times the other input's value. There's an animation further down that shows both happening on .
A common mistake. When a value is used in two places, it's tempting to follow just one path, or to let the second contribution overwrite the first, because each edge seems to give "the" derivative. It doesn't. Every use contributes, and the contributions add. In code that's the difference between `grad += ...` and `grad = ...`, and getting it wrong is one of the most common bugs in hand-written backprop.
See it
Worked examples
For at , , , run the forward pass and then the backward pass to find , and .
- 1
Break the formula into one operation per node: , then . So and feed , and and feed .
Let , and . Use the computation graph to find at .
- 1
Draw the graph: feeds both and , and both feed . So there are two paths from to , one through and one through .
Practice
Type your answer and check it. Solve them all to complete the lesson.
For at , , , find by backpropagating through the graph.
A graph computes , then , then . At , , , find .
Let , and . Add the contributions of both paths to find at .
In a computation graph, a weight is used by two different nodes on the way to the loss . How do you get ?
A smart thermostat predicts how many degrees a room will warm in the next hour as , where is the heater setting. It's trained with the squared error . On one reading, , , and the room actually warmed degrees. Draw the graph and find .
(Calculus 3 review.) Let with and . Use the multivariable chain rule, adding over both paths, to find at .
A network has one input, a hidden layer of 3 neurons, a second hidden layer of 4 neurons and one output, with every neuron connected to every neuron in the next layer. How many different paths run from the input to the output?
The hardest one here. Get it, and you've got this lesson.
A graph computes , , and . Notice that is used twice. At , , backpropagate to find .
Where it's used
Every deep learning library works exactly this way. As your code runs the forward pass, the library quietly records each operation as a node in a computation graph, along with the values it saw. When you ask for gradients, it walks that graph backward, multiplying local derivatives and adding at every node that was used more than once. You never write a derivative by hand, and the same machinery handles a two-node example and a model with billions of weights.