def sgd_2d(x1, x2, s1, s2, f_grad, eta, rng):
g1, g2 = f_grad(x1, x2)
n1, n2 = rng.normal(size=2) # Unit-variance gradient noise
return x1 - eta * (g1 + n1), x2 - eta * (g2 + n2), 0, 0Dive into Deep Learning · §9.2
Stochastic and Minibatch Gradient Descent
sampled gradients · stationary noise ball · Robbins–Monro decay · minibatch variance · excess loss · hardware throughput
Motivation
Stochastic Gradients
Valley Simulation
step 300, x1: -0.371201, x2: -0.077858
Second-Moment Recursion
Geometric Interpretation
Empirical Verification
eta=0.1: measured [0.2544 0.0157], predicted [0.2525 0.0156]
eta=0.05: measured [0.1275 0.007 ], predicted [0.1256 0.0069]
Decaying Step Sizes
Minibatch SGD
Minibatch SGD averages the gradient across b examples \mathcal{B}_t drawn at step t:
\mathbf{g}_t = \frac{1}{b}\sum_{i \in \mathcal{B}_t} \nabla f_i(\mathbf{x}_t)
Where Training Stalls
Exact Data Measurements
L = 2.10, tr Sigma = 3.24
Theory vs. Experiment
eta=0.05: excess loss 0.0042, predicted 0.0041
eta=0.025: excess loss 0.0021, predicted 0.0020
When Does the Noise Ball Vanish?
The excess-loss formula \mathbb{E}[f(\mathbf{x}_\infty)] - f(\mathbf{x}^\star) \approx \frac{\eta\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star)}{4b} reveals two fundamentally different training regimes depending on whether examples agree at \mathbf{x}^\star:
| Training Regime | Per-Example Gradients at \mathbf{x}^\star | Noise Trace \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star) | Constant-Rate SGD Behavior | Deep Learning Example |
|---|---|---|---|---|
| Underparameterized / Noisy-Label Regime | Examples disagree (\nabla f_i(\mathbf{x}^\star) \neq \mathbf{0}, only \frac{1}{n}\sum_i \nabla f_i(\mathbf{x}^\star) = \mathbf{0}) | \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star) > 0 | Stalls at excess loss \frac{\eta\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star)}{4b}; requires LR decay \eta_t \to 0 | Airfoil regression; single-pass LLM pretraining on massive corpora |
| Interpolating Regime | Model fits every training example exactly (\nabla f_i(\mathbf{x}^\star) = \mathbf{0} for all i) | \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star) = 0 | Noise ball shrinks automatically as \mathbf{x}_t \to \mathbf{x}^\star; converges geometrically | Overparameterized classifiers trained for many epochs to zero loss |
Practical Data Loaders
Real data loaders do not draw minibatches with replacement; they randomly permute the n examples each epoch and slice disjoint minibatches of size b:
Throughput Benchmark
b=1500: 1 updates/epoch, 0.003 s/epoch (2.7 ms/update), last-epoch loss 0.242
b= 1: 1500 updates/epoch, 1.825 s/epoch (1.2 ms/update), last-epoch loss 0.245
b= 10: 150 updates/epoch, 0.191 s/epoch (1.3 ms/update), last-epoch loss 0.245
b= 100: 15 updates/epoch, 0.023 s/epoch (1.5 ms/update), last-epoch loss 0.249
Progress per Example
Check Your Understanding
Calculate the outcome in each minibatch SGD scenario:
Check Your Understanding
Answers:
Key Takeaways
| Quantity / Law | Mathematical Formula | Scaling Dependence | Practical Implication |
|---|---|---|---|
| Minibatch Gradient Variance | \mathbb{E}\|\mathbf{g}_t - \nabla f\|^2 = \frac{\operatorname{tr}\boldsymbol{\Sigma}}{b}\frac{n-b}{n-1} | Variance \propto 1/b; standard deviation \propto 1/\sqrt{b} | 100\times larger batch cuts gradient error std by 10\times |
| Stationary Noise Ball | \mathbb{E}[x_\infty^2] = \frac{\eta\sigma^2}{b\lambda(2 - \eta\lambda)} \approx \frac{\eta\sigma^2}{2\lambda b} | Scales with ratio \eta/b and inversely with curvature \lambda | Flat directions fluctuate over the widest parameter radius |
| Stationary Excess Loss | \mathbb{E}[f(\mathbf{x}_\infty)] - f(\mathbf{x}^\star) \approx \frac{\eta\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star)}{4b} | Curvature \lambda_k cancels; proportional to \eta/b and \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star) | Vanishes in interpolating models (\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star)=0); requires LR decay otherwise |
| Robbins–Monro Conditions | \sum_t \eta_t = \infty,\; \sum_t \eta_t^2 < \infty | \eta_t = c/(t+t_0) gives \mathbb{E}\|\mathbf{x}_t - \mathbf{x}^\star\|^2 = O(1/t) | Infinite travel budget + finite cumulative noise variance |
| Linear Scaling Regime | \eta(b) \propto b for \eta L \ll 2 | Same progress per example and same noise floor at fixed \eta/b | Amortizes per-step overhead across parallel hardware up to b_{\text{crit}} (§9.8) |