Backpropagation
The algorithm that traverses the computation graph in reverse to compute every gradient in one pass.
The chain rule provides the formula for differentiating compositions. The computation graph records the structure of a forward pass. Backpropagation is the algorithm that combines them: it walks the graph in reverse and accumulates gradients at every node, computing all derivatives simultaneously in a single backward pass.
The Algorithm
Backpropagation operates in two phases.
Forward pass. Run the computation normally, building the graph as you go. Each node stores its output value and a backward function, the local gradient of that operation.
Backward pass. Starting from the output node, propagate gradients backward through the graph:
- Initialize the output node's gradient to . This is , which is trivially true.
- Visit each node in reverse topological order: process a node only after all of its consumers (nodes that used it as input) have already been processed.
- At each node: take the gradient that has accumulated there (the incoming gradient from all downstream nodes), apply the node's backward function to distribute it to each input, and add those contributions to the inputs' gradients.
- Repeat until all leaf nodes have been reached.
The reverse topological order guarantees that when we process a node, every contribution from downstream has already been accumulated. Nothing is double-counted or missed.
A Complete Walkthrough
Take with and . The forward pass produces: , , .
The backward pass proceeds as follows.
Step 1. Initialize: (the seed).
Step 2. Process . The local gradients are and . Distribute:
Step 3. Process . The local gradient is . Distribute:
Step 4. Process . The local gradients are and . Distribute:
Result. , . These match and computed analytically.
The gradient labels on each arrow show the value being passed backward. At , two arrows arrive and their contributions are summed: .
Gradient Accumulation
When a node is used in more than one operation, it receives gradient contributions from every path that passes through it. These contributions must all be summed.
This is the multi-variable chain rule in action. In the example above, appears in both and . The total effect of a small change in on the output is the sum of the two partial effects:
The graph makes accumulation automatic: whenever the backward function of a node distributes a gradient to an input, it adds to that input's accumulated total rather than overwriting it.
Why One Pass Is Enough
Computing gradients by finite differences costs one forward pass per parameter: to find , evaluate with slightly perturbed. For a model with parameters, that is forward passes.
Backpropagation costs one forward pass plus one backward pass, regardless of . The backward pass visits each node exactly once and does work proportional to the operation at that node, making its total cost comparable to a single forward pass.
For a model with one hundred million parameters, the speedup over numerical differentiation is on the order of fifty million. This is the computational argument that makes deep learning at scale feasible: the backward pass scales with the size of the graph, not with the number of parameters.
Connection to Deep Learning
In a neural network, every weight matrix is a leaf node with gradient tracking enabled. Every operation — matrix multiplication, addition of bias, activation function — adds interior nodes to the graph during the forward pass.
After calling .backward(), each weight matrix holds its gradient , which has the same shape as and contains for every entry. This is the complete sensitivity of the output to every single weight, computed in one traversal.
These gradients are then used to update each weight matrix. How that update is performed — the step size, the direction, whether to adjust the learning rate — is the subject of the Training section.
In Code
The same computation we walked through by hand:
import torch
x = torch.tensor([2.0], requires_grad=True)
y = torch.tensor([3.0], requires_grad=True)
a = x ** 2 # forward: a = 4, records ∂a/∂x = 2x
b = x * y # forward: b = 6, records ∂b/∂x = y and ∂b/∂y = x
z = a + b # forward: z = 10, records ∂z/∂a = 1 and ∂z/∂b = 1
z.backward() # backward pass: walks the graph in reverse, accumulating gradients
print(x.grad) # tensor([7.]) — 4 (through a) + 3 (through b)
print(y.grad) # tensor([2.]) — 2 (through b only)
The backward pass is complete. Every leaf now holds a gradient. For a network, that means every weight has received , a matrix encoding how each weight affects the output. What happens next — how those gradients are used to move the weights in a direction that improves the model — is what the Training section covers.