Answers · The mathematics underneath
Why does computing the gradient cost about the same no matter how many parameters the network has?
In shortBecause one backward sweep shares intermediate derivatives across all parameters, so the whole gradient costs a few forward passes in total rather than one per parameter.
The gradient costs about the same because backpropagation does not compute a separate derivative for each parameter. It makes one forward pass to keep the intermediate values and one backward sweep that carries a single row vector of derivatives from the loss back through every operation; the parameter derivatives fall out along the way. The cost is proportional to the number of operations and the size of the activations, not to the number of parameters.
The product is bracketed from the loss end
The chain rule fixes the factors in that product but not the order in which they are multiplied. Matrix multiplication is associative, so the answer is the same either way, but the cost is not. Multiplying from the right, starting with , means multiplying a matrix by a matrix, and the partial product stays wide because it has one column per parameter. Multiplying from the left, starting with , starts with a single row, because the loss is one number. A row vector times a matrix is a row vector, so every later product is again a single row. The chain never builds a matrix with one column per parameter. This is reverse mode, and it gives the derivative of one output with respect to all inputs in one sweep. Forward mode gives the derivative of all outputs with respect to one input per sweep, which is the wrong way round when there is one loss and billions of parameters.
| Method | Passes needed | Cost relative to one forward pass |
|---|---|---|
| Finite differences | one per parameter plus one | about |
| Forward mode | one per parameter | about |
| Reverse mode | one forward, one backward | a small constant |
A hand check
Take , , and target , with the chain , , , . Forward: , , , and . Those four numbers are all the forward pass has to produce, and all four are kept.
The backward sweep starts at with . At , . At , . At , , so . The parameter derivatives then fall out: , and . Each step is one multiplication by a value already computed on the forward pass. No extra pass is made for or .
What the guess misses
The natural guess is that parameters need derivatives, so the cost should grow with . Computing each derivative independently would indeed cost about forward passes by finite differences, or about sweeps by forward mode. The mistake is to treat the derivatives as separate objects. The derivative at is reused on the way to both and , and the derivative at is reused before that. The chain rule lets the same backward sweep supply every partial derivative, because all of them are entries of one row vector travelling back through the same operations. The row vector grows longer as the parameter count grows, but it is still one vector, and it is still produced by one sweep.
The price of that saving is memory rather than arithmetic. The local derivative of an operation depends on the values that went into it: the slope of at needs , and the derivative with respect to needs again. So the forward pass cannot discard its activations. Every intermediate value for every layer, and for every example in the batch, stays resident until the backward sweep has passed it. That is usually the binding constraint: the arithmetic of a backward pass is about twice the forward pass, while the memory is roughly the size of every intermediate value the model produced. Checkpointing trades some of that memory back for extra compute by throwing activations away and recomputing them during the sweep. The argument also depends on the loss being a single number. If there were many outputs and few inputs, reverse mode would no longer be the cheap direction, and forward mode would win instead.
The number of parameters changes the length of the row vector, not the number of sweeps. The cost is set by the number of operations and the size of the activations.
Worth keeping
- The gradient is a product of Jacobians, and the order of multiplication decides whether the cost grows with the parameter count.
- Multiplying from the loss end keeps every intermediate a row vector, so one backward sweep produces every partial derivative.
- The saving is paid for in memory, because each operation's local derivative needs the forward values that went into it.
Where this goes next
This question is one step of The Mathematics of Gradient Descent, a course that works through its subject from the beginning. The page that teaches this part of it is Where the Gradient Comes From, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why is the negative gradient the direction of steepest descent?A unit step goes downhill fastest when it points directly opposite the gradient, because the dot product with the gradient is then as negative as it can be.
- How does momentum stop the steps from cancelling each other out?Momentum keeps a running total of past gradients, so directions that keep agreeing build up while directions that keep flipping sign cancel out.
- Why does Adam need bias correction at the start of training?Adam needs bias correction because its two running averages start at zero, and without correction the early steps are several times larger than the learning rate asks for.
- Why does gradient descent still work when each step uses only one minibatch's gradient?A minibatch gradient is wrong at every step but correct on average, so the errors cancel over many steps while the true downhill pull keeps adding up.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.