Answers · The mathematics underneath
Why does gradient descent still work when each step uses only one minibatch's gradient?
In shortA 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.
Because the minibatch gradient is an unbiased estimate of the gradient of the full loss. Each individual step points somewhere slightly wrong, but the wrongness has no preferred direction, so across many steps those errors cancel while the true downhill pull keeps adding up.
The estimate and the thing it estimates
The full loss is an average over the training set, , so its gradient is the average of the per-example gradients. A minibatch gradient replaces the population average with an average over a random batch of size much smaller than .
The expectation is over which examples happen to land in the batch. It does not say the batch gradient is close to the full one: on thirty-two examples it can differ noticeably in both length and direction. It says the difference has mean zero. That is a claim about the centre of the scatter, not about its width.
Why the errors cancel
Unroll the run. After steps the parameters are the starting point minus the sum of the step directions taken, and each direction splits into the true gradient plus its error.
The first sum is ordinary descent and moves the parameters a distance of roughly after steps. The second sum is a sum of independent zero-mean terms, so it does not grow like ; it grows like a random walk, with standard deviation where is the per-example spread. Drift grows linearly in , noise grows as the square root, so the ratio of noise to progress falls as . Nothing accumulates in a wrong direction: it spreads. Put numbers in: take , and a batch of 16, so each step has error standard deviation . After 100 steps the drift is about and the accumulated error has standard deviation . The signal outweighs the accumulated noise twenty to one.
| relative noise | examples per step | examples per unit of precision |
|---|---|---|
| 1.0 | 1 | 1.0 |
| 0.5 | 4 | 2.0 |
| 0.25 | 16 | 4.0 |
| 0.125 | 64 | 8.0 |
The wrong guess
The natural reading is that a noisy gradient must give a wrong answer, that unless every step uses the exact gradient the run cannot settle at the true minimum. The first half is true and the second does not follow. Noise in the step is not noise in the function being minimised: the average step still points downhill, and where the run comes to rest is decided by where that average step vanishes, not by how widely the individual steps scatter around it. A bias behaves completely differently. If the errors had a mean of just in some fixed direction, the steps would stop where equals minus that bias, and the run would settle permanently beside the minimum. More steps would not help, because more steps make a bias more decisive, not less. This is why drawing the batch uniformly matters: a batch that over-represents easy examples gives a biased gradient, and a biased gradient minimises a different function from the one that was written down.
Where it stops being true
The cancelling argument works on the way down, while the true gradient is large. Near a minimum the true gradient shrinks toward zero and the noise does not, because the disagreement between examples is still there when their average is zero. With a fixed step size the run then settles into a region around the minimum rather than at it, and the size of that region is proportional to the step size.
Halving the step size halves that floor, which is why a curve that has flattened for a long time often drops immediately when the learning rate is cut, and why the drop is abrupt rather than gradual. It also stops being true at very large batches: doubling the batch halves the variance of each step but also halves the number of steps per pass over the data, and the two effects roughly cancel if the step size is doubled at the same time, keeping fixed. That rule holds over a wide range and then fails, because the step size it demands runs into the ceiling set by curvature, which knows nothing about batch size. Warmup schedules exist to manage that collision.
Worth keeping
- A minibatch gradient is centred on the true gradient, so its errors cancel across steps instead of steering the run.
- The noise in a minibatch gradient falls as one over the square root of the batch size, so quadrupling the batch only halves it.
- With a fixed step size a run settles in a region around the minimum whose size is proportional to the step size, not at the minimum itself.
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 Why a Noisy Gradient Still Works, 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 computing the gradient cost about the same no matter how many parameters the network has?Because 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.
- 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.
- 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.