3.1 — Batch, stochastic, and mini-batch gradient descent
Goal: Understand the gradient estimator at each batch size, its bias and variance, and why mini-batches are the universal compromise.
1. Why this session exists
In Chapter 1 we wrote the update as if were a single, computable thing. For modern training that is a fiction. The loss is an average over examples — for MNIST, tokens for a frontier LM — and computing the true gradient once per step is prohibitive.
So we estimate it. The estimator's choice — full batch, single example, or a mini-batch of size — controls bias, variance, memory, and how often we get to update. This session sets up the bias/variance picture and locks in the vocabulary for the rest of Chapter 3.
2. The empirical risk and its true gradient
The training objective is the empirical risk
with per-example loss . Its gradient is the average
Call the per-example gradient. Then , the population mean of the 's. Every estimator we write next is some attempt at .[3]
3. The three regimes
Full-batch GD (BGD). Use all examples per step:
This is the exact gradient. No noise, but each step costs forward+backward passes. For that is one step per several minutes; impossible.
Stochastic GD (SGD). Pick one example uniformly at random and use only that gradient:
One forward+backward per step; we update parameters times per pass through the dataset. Maximum noise, maximum step count.
Mini-batch SGD. Pick a random subset with :
Memory and compute scale linearly with ; noise scales as (we'll prove this in §4). For to on a GPU, mini-batch GD is what every modern trainer runs.[1, 2, 4, 5]
4. Bias and variance of the estimator
Sample uniformly at random (with replacement, to make the math clean — without-replacement is the same up to corrections). Define the per-example gradient as a random variable taking value with probability .
Mean (bias).
So is unbiased for the true gradient. No bias correction needed — the estimator is honest in expectation, at every from to . SGD () is unbiased; full-batch () is unbiased and noise-free.
Variance. Let , the covariance matrix of the per-example gradients. With i.i.d. samples,
The scalar quantity we care about is the squared error of the estimator,
So
Doubling the batch size halves the variance but doubles the compute per step. To halve the noise std you need four times the batch.
5. Why noise can help
The naive read is "less variance is better, use full-batch". Two reasons that's wrong.
(a) Saddle escape. In a high-dimensional non-convex loss, saddle points vastly outnumber local minima (Dauphin et al., 2014). Full-batch GD on a saddle stays exactly there — . The noise of SGD perturbs off the saddle into a descent direction. Stochasticity is a feature.
(b) Implicit regularisation. A line of work (Keskar et al., 2017; Smith & Le, 2018) argues that large-batch GD converges to sharp minima — narrow valleys where small parameter perturbations cause large loss changes — and that these generalise worse than the flat minima preferred by small-batch SGD. The noise effectively acts like a low-pass filter on the loss landscape, biasing the optimiser toward wide basins. We will revisit this in 3.9 and 6.12 in the context of LLM training.
So variance is not pure cost; some of it is doing useful work.
6. Step cost vs total compute
Three knobs you can spend compute on: bigger batches, more steps, or a different optimiser. The mini-batch arithmetic gives a clean way to compare.
Let be the per-step cost (linear in — one forward+backward per example, GPU-parallelised). After steps the total compute is . To reach a target loss, full-batch needs few but expensive steps; SGD needs many but cheap steps. For convex objectives, an exact bound (Bottou et al., 2018) is
with the strong-convexity constant. The first term shrinks as (variance-dominated regime; helps to increase ); the second is independent of (bias-dominated regime; increasing is wasted). There is a critical batch size past which more batch is wasted compute. Practical takeaway: very small or very large batches are both suboptimal; the sweet spot is data-dependent and what every modern trainer empirically tunes.
7. Epoch, step, iteration: vocabulary
- Step (or iteration). One parameter update: one forward+backward on one mini-batch.
- Epoch. One full pass through the training set: steps.
- Sample without replacement per epoch. Practical implementations shuffle the dataset at the start of each epoch and walk through it in order. This is technically biased (within an epoch, samples are dependent) but the bias is negligible for .
The convention we will follow: "training for steps with batch " is unambiguous; "training for epochs" implies steps. Both appear in papers.
8. Takeaways
- Mini-batch SGD is unbiased for the true gradient: .
- Variance scales as ; noise std as .
- Pure SGD () is maximally noisy but cheap; full batch is exact but expensive.
- Noise is partly useful: it escapes saddles and biases toward flat minima.
- There is a problem-dependent critical batch size ; below it, doubling the batch nearly halves time-to-target; above it, the extra batch is wasted.
(a) Show by direct calculation that for i.i.d. per-example gradients with covariance , .
(b) A trainer reports that doubling batch from to doubles wall-clock per step but reduces the number of steps to a given loss by only ~. What does this say about whether is above or below the critical batch size?
(c) Why is full-batch GD a particularly bad optimiser at saddle points? Set up a 2-D example: at . What does BGD do? What does SGD do (assume i.i.d. noise added to the gradient)?
(d) Empirically, you sometimes see "linear scaling rule": when you multiply batch by , also multiply learning rate by . Argue (informally) that this keeps the per-epoch parameter trajectory roughly invariant: write the expected change in after one epoch and show it is approximately the same.
References
- [1]
Barbaresi, A. (2021). "Trafilatura: a web scraping library and command-line tool for text discovery and extraction". ACL System Demonstrations. aclanthology.org/2021.acl-demo.15 — Reference HTML→text extractor (7.1 §3.1).
↩ - [2]
Child, R., Gray, S., Radford, A., & Sutskever, I. (2019). "Generating long sequences with sparse transformers". arXiv:1904.10509 — Sparse Transformer's alternating sparse-dense pattern; used in GPT-3 (7.14 §3.1).
↩ - [3]
Radford, A., Wu, J., Child, R., et al. (2019). "Language models are unsupervised multitask learners" (GPT-2). OpenAI technical report. — Pre-LN block, modified init, reversible tokenization referenced by GPT-3 §2.1 (7.14 §3.1).
↩ - [4]
Fedus, W., Zoph, B., & Shazeer, N. (2021). "Switch Transformer: Scaling to trillion parameter models with simple and efficient sparsity". arXiv:2101.03961 — Top-1 routing; standard auxiliary loss formulation (8.6 §3.1).
↩ - [5]
Gu, A., Goel, K., & Ré, C. (2022). "Efficiently modeling long sequences with structured state spaces" (S4). arXiv:2111.00396 — Structured (diagonal-plus-low-rank) for efficient long-sequence SSM (8.7 §3.1).
↩