Foundations to Frontier
Tests
A. Mathematical Foundations › A6 Optimization › Optimization for ML

Stochastic Gradient Descent and Mini-Batching

When the objective is an average over millions of data points, computing the exact gradient every step is wasteful. Stochastic gradient descent uses the gradient on a small random mini-batch instead: unbiased, cheap, noisy. You will learn why SGD works, how mini-batch size controls gradient variance (σ²/B), why a constant step leaves SGD bouncing in a noise ball while decaying steps (Robbins–Monro) converge, practical choices (shuffling, epochs, the linear-scaling rule), and measure how SGD beats full-batch GD per unit of compute on a terrain classifier.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Ask a few, not everyone

To find which way a crowd of a million people wants to move, you could poll everyone, which is slow and exact, or ask 32 random people, which is quick and roughly right. Repeating the quick poll many times and taking a small step each time gets you there far faster overall. Each step is a little wrong, but the errors average out.

That’s stochastic gradient descent: estimate the downhill direction from a small random mini-batch of data, step, and repeat.

Gradient descent with noisy, cheap gradients: SGD
slope a →intercept b
gradient evaluations: GD 60000 vs SGD 1200 · SGD final loss − optimum = 9.35e-3 · noise-ball radius ≈ 0.173
Smooth path: full-batch GD (every step touches all 200 points). Orange: SGD using B random points per step. It's B/200 the cost per step, and on average it points the right way, but it jitters. With a constant step it never settles; it hovers in a noise ball (dashed) whose size grows with η and shrinks like 1/√B. A decaying step (Robbins–Monro) shrinks the ball to zero. Too large a step diverges.
Why engineers care

Every neural network, from perception models on rovers to large language models, is trained with SGD or a variant. Choosing the batch size, step size and schedule determines training time, cost and even how well the model generalizes.

📝 Quick check (Level 0)
0/3 answered
  1. 1. SGD estimates the gradient from…
  2. 2. Each SGD step is…
  3. 3. Over many steps, the errors in SGD's gradient estimates…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The setting

Empirical risk: f(w)=1n∑i=1nℓi(w)f(w) = \frac1n\sum_{i=1}^n\ell_i(w).

  • Full gradient costs nn sample gradients.
  • Mini-batch gradient: gB(w)=1B∑i∈B∇ℓi(w)g_B(w) = \frac1B\sum_{i\in\mathcal B}\nabla\ell_i(w) with a random batch B\mathcal B of size BB.
  • It’s unbiased: E[gB]=∇f(w)\mathbb E[g_B] = \nabla f(w).
  • Its covariance is Σ/B\Sigma/B, where Σ\Sigma is the per-sample gradient covariance.

The update

wk+1=wk−ηk gBk(wk)w_{k+1} = w_k - \eta_k\,g_{\mathcal B_k}(w_k)

In practice: shuffle the data each epoch (one pass) and take n/Bn/B steps per epoch.

📝 Quick check (Level 1)
0/3 answered
  1. 1. The mini-batch gradient is…
  2. 2. Its variance scales as…
  3. 3. An epoch is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Constant vs decaying steps

With a constant step, SGD converges quickly at first, then stalls in a noise ball: the steady-state error is O(ησ2/μ)O(\eta\sigma^2/\mu) for μ\mu-strongly convex problems. Halving η\eta or quadrupling BB shrinks the ball.

To converge exactly, the steps must decay. The Robbins–Monro conditions are ∑ηk=∞\sum\eta_k = \infty (enough total movement) and ∑ηk2<∞\sum\eta_k^2 < \infty (the noise dies out). With ηk=c/kp\eta_k = c/k^p, that means 12<p≤1\frac12 < p\le1.

Rates (convex, bounded variance)

  • Strongly convex, ηk∝1/k\eta_k\propto1/k: E[f]−f⋆=O(1/k)\mathbb E[f] - f^\star = O(1/k).
  • Convex, ηk∝1/k\eta_k\propto1/\sqrt k: O(1/k)O(1/\sqrt k).

That’s slower per step than GD, but each step costs BB instead of nn. For large nn, SGD wins per unit of compute. Proofs are in A6.3-2.

Practical choices

  • Batch size: larger BB means less noise and better hardware parallelism, but beyond a critical batch size returns diminish (McCandlish et al., 2018).
  • Linear scaling rule: multiply η\eta by kk when BB is multiplied by kk, with a warmup (Goyal et al., 2017).
  • Shuffling (sampling without replacement) usually beats sampling with replacement.
  • Generalization: SGD’s noise biases it toward flatter minima, a form of implicit regularization (Keskar et al., 2017).
🐍 Exercise 1: noise balls and batch variance
Implement mini-batch gradients and SGD with reshuffling. Show that a constant step stalls at a noise floor that grows with the step, that decay shrinks it, and that mini-batch variance falls like 1/B.
📝 Quick check (Level 2)
0/3 answered
  1. 1. With a constant step, SGD on a strongly convex problem…
  2. 2. Robbins–Monro steps ηₖ = c/kᵖ need…
  3. 3. The linear scaling rule says, when B doubles…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Unbiasedness and variance

With B\mathcal B drawn uniformly, E[1B∑i∈B∇ℓi]=1B⋅B⋅1n∑i∇ℓi=∇f\mathbb E\big[\frac1B\sum_{i\in\mathcal B}\nabla\ell_i\big] = \frac1B\cdot B\cdot\frac1n\sum_i\nabla\ell_i = \nabla f. With i.i.d. draws (with replacement), the variance of an average of BB independent terms is Σ/B\Sigma/B. Without replacement it’s ΣB⋅n−Bn−1\frac\Sigma B\cdot\frac{n - B}{n - 1}, which is zero at B=nB = n. ∎

The noise ball for a 1-D quadratic

Take f=μ2w2f = \frac\mu2w^2 with gradient noise ξk\xi_k of variance σ2\sigma^2: wk+1=(1−ημ)wk−ηξkw_{k+1} = (1 - \eta\mu)w_k - \eta\xi_k. The stationary variance vv satisfies v=(1−ημ)2v+η2σ2v = (1 - \eta\mu)^2v + \eta^2\sigma^2, so

v=ησ2μ(2−ημ)≈ησ22μv = \frac{\eta\sigma^2}{\mu(2 - \eta\mu)}\approx\frac{\eta\sigma^2}{2\mu}

It’s proportional to the step size and to the noise variance σ2∝1/B\sigma^2\propto1/B. ∎

Why steps must decay

If ∑ηk2=∞\sum\eta_k^2 = \infty, the accumulated noise ∑ηkξk\sum\eta_k\xi_k has unbounded variance. If ∑ηk<∞\sum\eta_k < \infty, the iterates may stop short of the optimum. Robbins and Monro (1951) proved almost-sure convergence under both conditions.

Exercise 3.1: Stationary variance for η = 0.1, μ = 1, σ² = 1

v=0.1/(1⋅1.9)≈0.0526v = 0.1/(1\cdot1.9)\approx0.0526. Halving η\eta to 0.05 gives 0.05/1.95≈0.02560.05/1.95\approx0.0256, about half.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Without replacement, the batch-mean variance at B = n is…
  2. 2. The stationary variance of constant-step SGD on a quadratic is about…
  3. 3. If Σηₖ < ∞, SGD may…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Training a terrain classifier on a fixed compute budget

A rover classifies terrain (safe vs hazardous) from 8 IMU/camera features using logistic regression on 20,000 labelled samples. Give each method the same budget: 40,000 per-sample gradient evaluations, two passes’ worth. Full-batch GD gets two steps and has barely moved. Mini-batch SGD (B = 32) gets 1250 steps and lands within about 0.002 of the optimal loss. When data are plentiful and redundant, SGD wins per unit of compute. That’s the foundation of all large-scale ML (Bottou and Bousquet, 2008).

🐍 Exercise 2 (research): SGD vs GD on a compute budget
Train logistic regression with full-batch GD and mini-batch SGD under the same budget of sample-gradient evaluations, and compare their losses with the optimum.

SGD in AI, robotics and aerospace

  • Deep learning at every scale: SGD with momentum and Adam (A6.3-3, A6.3-4) train perception, control and language models.
  • Online learning on robots: adaptive control and system identification update parameters per measurement, SGD-style (LMS; Widrow and Hoff, 1960).
  • Large-batch training across GPU/TPU clusters relies on the linear-scaling and warmup rules (Goyal et al., 2017; You et al., 2020, LAMB).
  • Stochastic approximation underpins TD learning and Q-learning in RL (Sutton and Barto, 2018).

References

  1. H. Robbins, S. Monro (1951). “A stochastic approximation method.” Annals of Mathematical Statistics, 22(3).
  2. L. Bottou, O. Bousquet (2008). “The tradeoffs of large scale learning.” NeurIPS.
  3. L. Bottou, F. Curtis, J. Nocedal (2018). “Optimization methods for large-scale machine learning.” SIAM Review, 60(2).
  4. A. Nemirovski, A. Juditsky, G. Lan, A. Shapiro (2009). “Robust stochastic approximation approach to stochastic programming.” SIAM Journal on Optimization, 19(4).
  5. P. Goyal et al. (2017). “Accurate, large minibatch SGD: training ImageNet in 1 hour.” arXiv:1706.02677.
  6. S. McCandlish, J. Kaplan, D. Amodei (2018). “An empirical model of large-batch training.” arXiv:1812.06162.
  7. N. Keskar et al. (2017). “On large-batch training for deep learning: generalization gap and sharp minima.” ICLR.
  8. B. Widrow, M. Hoff (1960). “Adaptive switching circuits.” IRE WESCON Convention Record.
  9. Y. You et al. (2020). “Large batch optimization for deep learning: training BERT in 76 minutes.” ICLR.
  10. R. Sutton, A. Barto (2018). Reinforcement Learning: An Introduction, 2nd ed. MIT Press.