Neural networks · lesson 6 of 10
Parts in this lesson
An2_grad = f_grad * aa node passes its gradient back, times its local derivative
Ba_grad += n1_grad * bgradients from every path add up
CDual(value, deriv)forward mode: a number that carries its derivative

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 f=(ab+c)a. To work it out, a computer does three small operations, in order: multiply a by b; add c; multiply by a. 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.

graph.ipynb

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 n=u+v passes gradients through unchanged: ∂n/∂u=1;
  • a multiplication n=uv swaps its inputs: ∂n/∂u=v and ∂n/∂v=u;
  • a function like tanh⁡ or ReLU multiplies by its slope (lesson 6.2).

The backward pass starts at the result with ∂f/∂f=1 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:

Graph stepperforward in ink, backward in pink
a2grad ·b−3grad ·×?grad ·c10grad ·+?grad ·×?grad ·

Step 0 of 7. Nothing worked out yet. Press Step to start the forward pass.

Gradients add where paths meet

a appears twice: once in ab, and once in the final multiplication. So a nudge to a changes f 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.

∂f∂a=n2+ab=4+(2)(−3)=−2

The first term, n2, is the path through the last multiplication. The second, a⋅b, is the path through ab: the last × passes back a, the + passes it on unchanged, and the first × multiplies it by b.

In code, a node's gradient starts at 0 and every path adds to it with +=:

graph.ipynb

The backward pass gives −2, 4 and 2, and nudging agrees. In the stepper above, watch a 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, W⊤δ.

Wrong way: forgetting to add

Write the second contribution to a_grad with = instead of +=:

graph.ipynb

−6 instead of −2: 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:

graph.ipynb

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:

Forward or reverse?passes needed for every derivative
Forward mode1 million passesReverse mode1 passbars on a log scale: each tick of length is 10 times more

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

In f = (ab + c)a with a = 2, b = −3, c = 10, what gradient does the + node (n₂ = n₁ + c) pass to c?
A value feeds three different nodes. How is its gradient worked out?
A function has 3 inputs and 1,000 outputs, and you need every derivative. Which mode needs fewer passes?

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.

All sources for Simple Machine Learning