Computational graphs
Last updated
Lesson 6.4 derived a network’s gradients by hand, layer by layer. That doesn’t scale: every new kind of layer would need new algebra. The way out is to see any formula as a graph of small operations, each of which knows only its own local derivative. Then one simple procedure, run backwards over the graph, finds every gradient of anything. This lesson draws formulas as graphs, steps through them forwards and backwards, and finds the one rule that’s easy to forget.
By the end of this lesson you can
- Draw a formula as a graph of operations, with values flowing forward
- Run the backward pass on a graph, multiplying by each node’s local derivative
- Explain why gradients add where a value is used more than once
- Say how forward mode and reverse mode differ, and why training uses reverse mode
Nodes and edges
Take the formula . To work it out, a computer does three small operations, in order: multiply by ; add ; multiply by . Draw each operation as a node, and draw an edge from each value to the operations that use it. That's a computational graph: inputs on the left, the result on the right, and every value in between with a name.
Working out the nodes from left to right is the forward pass. Any formula, however long, and any network, however deep, is a graph like this; a network's is just bigger, with matrix products and activations as its nodes.
Local derivatives
Each node only needs to know one thing for the backward pass: its local derivative, how its output changes when each of its inputs changes, ignoring the rest of the graph. These are all short:
- an addition passes gradients through unchanged: ;
- a multiplication swaps its inputs: and ;
- a function like or ReLU multiplies by its slope (lesson 6.2).
The backward pass starts at the result with and visits the nodes in reverse order. Each node takes the gradient that has reached it, multiplies it by each local derivative, and sends the products to its inputs. That's the chain rule (lesson 1.7), one edge at a time. Step through it:
Step 0 of 7. Nothing worked out yet. Press Step to start the forward pass.
Gradients add where paths meet
appears twice: once in , and once in the final multiplication. So a nudge to changes along two paths, and its total effect is the sum of the two. The chain rule for several paths says exactly that: add the contributions.
The first term, , is the path through the last multiplication. The second, , is the path through : the last passes back , the passes it on unchanged, and the first multiplies it by .
In code, a node's gradient starts at 0 and every path adds to it with +=:
The backward pass gives , 4 and 2, and nudging agrees. In the stepper above, watch get its gradient in two instalments, one from each node that uses it. In a network, this happens constantly: every hidden unit feeds every unit in the next layer, which is why lesson 6.4's hidden gradient was a sum, .
Wrong way: forgetting to add
Write the second contribution to a_grad with = instead of +=:
instead of : the first path's contribution was thrown away, and nothing complained. It's the single commonest bug in hand-written backward passes, and in autograd libraries too, which is why lesson 6.7's Value class accumulates every gradient with +=, and why it needs a way to reset them to 0 between training steps.
Forward mode against reverse mode
There's a second way to run derivatives through a graph. Forward mode carries a derivative along with each value, left to right: every number becomes a pair (value, derivative with respect to one chosen input). The rules are the same local derivatives. In Python, a small class does it:
Same answers. But forward mode found one input's derivative per pass: three inputs, three passes. Reverse mode, the backward pass, found all three in one pass, because it carries one output's gradient back to every input at once.
So which mode is cheaper depends on the shape of the problem:
1 million inputs, 1 output: forward mode needs 1 million passes, reverse mode 1. A loss is one number, so reverse mode gets every weight's gradient in a single backward pass. That's backpropagation.
Training has one output, the loss, and as many inputs as the network has weights: millions, or billions. Reverse mode gets them all for about the cost of one forward pass. That's why backpropagation is reverse-mode automatic differentiation, and why every deep learning library is built around it. The price is memory: the backward pass needs every value from the forward pass, so they must all be kept until it's done.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Baydin, Pearlmutter, Radul and Siskind, Automatic differentiation in machine learning: a survey, Journal of Machine Learning Research 18, 2018: computational graphs, forward and reverse mode, and dual numbers
- Goodfellow, Bengio and Courville, Deep Learning, section 6.5: Back-Propagation and Other Differentiation Algorithms: computational graphs and the general back-propagation algorithm
- Prince, Understanding Deep Learning, chapter 7: Gradients and initialization: the backward pass as reverse-mode differentiation