Full gradient descent needs the gradient of the loss over the entire dataset at every step — an sweep per update. Modern datasets made that absurd, and the fix changed the character of the algorithm: estimate the gradient from a random minibatch, and accept that every step is now a draw from a distribution. This lesson treats SGD as what it is — a stochastic process — because that is the only frame in which its behaviour (fast early, noisy late, schedule-dependent forever) makes sense.
The estimator
The training loss is a finite sum, , one term per example. Sampling a minibatch of size uniformly and averaging gives the minibatch gradient
which is unbiased — — with covariance shrinking linearly in the batch size:
where is the covariance of a single-example gradient. Two consequences before any dynamics: the direction is right on average, and averaging four times more examples only halves the noise — standard-error scaling, . Batch size buys noise reduction at a steep, sublinear exchange rate.
The update is otherwise unchanged:
That decomposition — gradient step plus zero-mean kick scaled by the learning rate — is the whole algorithm. Everything below is about the tug-of-war between the two terms.
idx = rng.permutation(n) # each epoch: shuffle, then slice
for s in range(0, n, b):
batch = idx[s : s + b]
x -= lr(t) * grad(x, batch) # unbiased, noisy, cheap
t += 1
The noise ball: what a constant learning rate converges to
Far from the optimum, the true gradient dwarfs the noise and SGD behaves like slightly drunk gradient descent — early progress is nearly as fast, at -th the cost per step. Near the optimum the gradient vanishes but the noise does not, and the two terms balance. For a -strongly convex, -smooth loss with (bounded) gradient-noise variance , constant- SGD satisfies, in the limit,
— it does not converge. It reaches a stationary distribution: a fuzzy cloud around the minimum, the noise ball, whose radius scales with and . On the loss curve this is unmistakable: a fast drop, then a plateau that no amount of further training lowers. The plateau is not a bug, a bad init, or a local minimum — it is the equilibrium radius of a stochastic process, and the only levers that lower it are the ones in the formula: smaller , bigger , or less noise.
Run it, then let it sit: the iterate orbits the minimum in a cloud whose radius you control. Halve the learning rate — the cloud tightens and the crawl slows. Raise the batch size from 8 to 64 — same effect, bought with compute instead of speed. Switch the schedule to and the ball itself shrinks over time: that is the schedule doing precisely its theoretical job. On the two-wells surface, turn σ up and watch the iterate occasionally kick out of the shallow well entirely — remember that sight when you reach the non-convex lesson.
Schedules: how to shrink the ball
If a constant ends in a ball of radius , the classical move is to decay it. The Robbins–Monro conditions (1951 — SGD is older than the transistor) say the schedule must satisfy
— enough total step to travel anywhere, but square-summable so the accumulated noise is finite. qualifies; fails the second condition but achieves the optimal convex rate with averaging, and is what practice actually approximates. The realities of deep learning added an engineering layer on top:
| Schedule | Shape | When it earns its keep |
|---|---|---|
| Step decay | ÷10 at fixed epochs | classic vision training; crude but effective |
| Cosine | smooth decay to ~0 | the modern default; no thresholds to tune |
| Warmup → decay | ramp up, then decay | large-batch and transformer training, where a full-size at step 0 diverges |
| Constant + late averaging | flat, then average iterates | theory-optimal for convex SGD (Polyak–Ruppert averaging) |
The unifying logic is exactly the noise ball: large early to cross the landscape while gradients dominate; small late to shrink the equilibrium cloud once noise dominates. Every schedule is a policy for spending that budget.
Batch size is not a free parameter
The variance formula tempts a simple story — bigger batch, less noise, better — but the exchange rates matter:
- Compute per update grows linearly in , while noise shrinks only as . Past a problem-dependent critical size, extra examples per batch stop buying wall-clock progress (Shallue et al. call this the end of “perfect scaling”).
- The linear-scaling rule — when you multiply by , multiply by too (Goyal et al., training ImageNet at ) — falls straight out of the update: small noisy steps ≈ one -times-larger, -less-noisy step, provided the gradient does not change much across them. It breaks near the stability ceiling from the previous lesson, which is why large-batch recipes need warmup.
- The noise is not purely adversarial. Small-batch SGD reliably finds solutions that generalize slightly better in many settings (Keskar et al.); one mechanism is that the noise ball cannot fit inside sharp, narrow minima, so the process preferentially settles in wide, flat basins. The full flat-vs-sharp story — and its caveats — is picked up in the non-convex landscapes lesson, but the operational summary stands: the noise is a regularizer you were given for free, and “reduce it to zero” is not automatically the goal.
A reproducibility note practitioners learn the hard way
SGD’s result is a random variable: it depends on the shuffling seed, and on GPUs even “identical” runs differ through non-deterministic kernels and float addition order. Report loss curves across seeds, not a single lucky run — variance across seeds is often the honest error bar on every number in your results table.
Exercises
Work these before the next lesson
- Prove the unbiasedness and the covariance for sampling with replacement. What changes without replacement, and why does the distinction vanish for ?
Solution
Worked solutions are part of Premium — unlock all of them for £5/month →
- 4 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →
References
- H. Robbins & S. Monro, “A Stochastic Approximation Method”, Ann. Math. Stat. 1951 — the original, still readable.
- L. Bottou, F. Curtis, J. Nocedal, “Optimization Methods for Large-Scale Machine Learning”, SIAM Review 2018 — the definitive modern survey; §4 covers the noise-ball analysis quoted here.
- P. Goyal et al., “Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour”, 2017 — the linear-scaling rule and warmup.
- N. S. Keskar et al., “On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima”, ICLR 2017.
- C. Shallue et al., “Measuring the Effects of Data Parallelism on Neural Network Training”, JMLR 2019 — the critical-batch-size measurements.