Computational Graphs and the Chain Rule
Backpropagation is the chain rule of calculus applied systematically to a computational graph: a directed acyclic graph whose nodes are elementary operations and whose edges carry values. Drawing the graph turns gradient computation into a mechanical procedure: each node only needs to know its own local derivative. The calculus is reviewed in Backpropagation and Backpropagation and Autodiff.
Building the graph
Take . Introduce an intermediate , so . Every operation is a node:
The forward pass evaluates nodes in topological order. The backward pass visits them in reverse, starting from , and each node multiplies the incoming gradient (the upstream gradient) by its local gradient:
x, y, z = -2.0, 5.0, -4.0
# forward
q = x + y
f = q * z
# backward
df = 1.0
dq, dz = z * df, q * df # multiply node: swap the inputs
dx, dy = 1.0 * dq, 1.0 * dq # add node: pass the gradient through
print(f"f = {f}")
print(f"df/dx = {dx}, df/dy = {dy}, df/dz = {dz}")f = -12.0
df/dx = -4.0, df/dy = -4.0, df/dz = 3.0
Gate patterns
The local rules of a few nodes cover most of backprop:
| Node | Forward | Backward (upstream ) |
|---|---|---|
| add | both inputs receive | |
| multiply | receives , receives | |
| max | the larger input receives , the other 0 | |
| copy (fan-out) | used twice | the gradients from both uses add |
| matrix multiply | , |
The fan-out rule is the multivariate chain rule: if a value feeds several nodes, its total derivative is the sum over all paths.
A sigmoid neuron, node by node
breaks into multiplies, adds, a negation, an exponential, a and a reciprocal. Backprop through all of them gives the same answer as the shortcut : you may group nodes into bigger ones whenever their local derivative is simple.
import numpy as np
w0, x0, w1, x1, w2 = 2.0, -1.0, -3.0, -2.0, -3.0
# forward, one elementary node at a time
a = w0 * x0 + w1 * x1 + w2
n1 = -a; n2 = np.exp(n1); n3 = 1 + n2; f = 1 / n3
# backward, one node at a time
d3 = -1 / n3**2 # d(1/u)/du
d2 = d3 * 1 # +1
d1 = d2 * np.exp(n1) # exp
da = d1 * -1 # negation
print(f"f = {f:.4f}")
print(f"node-by-node da = {da:.4f}, shortcut f(1-f) = {f * (1 - f):.4f}")
print(f"dw0 = {da * x0:.4f}, dx0 = {da * w0:.4f}, dw1 = {da * x1:.4f}, dx1 = {da * w1:.4f}, dw2 = {da:.4f}")f = 0.7311
node-by-node da = 0.1966, shortcut f(1-f) = 0.1966
dw0 = -0.1966, dx0 = 0.3932, dw1 = -0.3932, dx1 = -0.5898, dw2 = 0.1966
Why reverse order is efficient
For a scalar loss and parameters, one backward pass gives all partial derivatives for a cost of a small constant times one forward pass. Computing them one at a time by finite differences would take forward passes. This is the reason networks with billions of parameters can be trained at all (Forward and Reverse Mode).
In software
- PyTorch builds the graph dynamically as operations run ("define by run");
loss.backward()walks it in reverse.tensor.grad_fnshows the node that produced a tensor. - JAX traces a function into a graph (a jaxpr) and transforms it with
grad. torch.autograd.gradcheckcompares backprop to finite differences.
Exercises
Exercise 1: Fan-out
written as a multiply node with both inputs from the same . Use the gate rules to find .
Answer
.
Solution
The multiply node sends to each input; both inputs are the same variable, so the contributions add: .
Exercise 2: Max gate
with , , . Find all three partial derivatives.
Answer
, , .
Solution
. The multiply node gives and . The max routes 2 to (the larger input) and 0 to .
Exercise 3: Check against finite differences (code)
For , backpropagate by hand at and compare with central differences.
Answer
Both give and .
Solution
With , , : , , so and (fan-out of ).
import numpy as np
x, y = 1.0, 0.5
q = x + 2 * y; s = q**2; f = s * np.sin(x)
ds = np.sin(x); dq = ds * 2 * q
dx = dq * 1 + s * np.cos(x); dy = dq * 2
F = lambda x, y: (x + 2 * y)**2 * np.sin(x); h = 1e-6
print(round(dx, 4), round(dy, 4), round((F(x + h, y) - F(x - h, y)) / (2 * h), 4), round((F(x, y + h) - F(x, y - h)) / (2 * h), 4))5.5271 6.7318 5.5271 6.7318
Test yourself
In the backward pass, an add node…
- swaps its inputs
- passes the upstream gradient unchanged to both inputs
- sends 0 to both inputs
- doubles the gradient
When a value is used by two later nodes, its gradient is…
- the larger of the two incoming gradients
- the sum of the incoming gradients
- the product of the incoming gradients
- always zero
Reverse-mode differentiation of a scalar loss with parameters costs about…
- forward passes
- a small constant times one forward pass
- forward passes
- nothing