Stochastic and Minibatch Gradient Descent

Dive into Deep Learning · §9.2

Stochastic and Minibatch Gradient Descent
sampled gradients · stationary noise ball · Robbins–Monro decay · minibatch variance · excess loss · hardware throughput

Why Full-Batch Gradient Descent Is Prohibitive

Motivation

  • Full-batch cost explosion: Evaluating \nabla f(\mathbf{x}) = \frac{1}{n}\sum_{i=1}^n \nabla f_i(\mathbf{x}) over ImageNet (n = 1.28\text{M}) for 450{,}000 updates would require 450{,}000 full dataset passes (5{,}000\times more compute than 90 epochs at b = 256).
  • Minibatch efficiency (b = 256): Estimates the downhill direction at 1/5{,}000th the cost per step.
  • The fundamental trade-off: Sampling b \ll n examples injects gradient noise that traps constant-rate iterates in a stationary noise ball.

Unbiased Sampled Gradients and the Noise Decomposition

Stochastic Gradients

  • Unbiased estimator: Drawing i \sim \text{Uniform}\{1, \ldots, n\} gives \mathbb{E}_i[\nabla f_i(\mathbf{x})] = \nabla f(\mathbf{x}).
  • Signal-plus-noise decomposition: \nabla f_i(\mathbf{x}_t) = \nabla f(\mathbf{x}_t) + \boldsymbol{\xi}_t, \qquad \mathbb{E}[\boldsymbol{\xi}_t \mid \mathbf{x}_t] = \mathbf{0}
  • Non-vanishing noise at \mathbf{x}^\star: Even where \nabla f(\mathbf{x}^\star) = \mathbf{0}, individual gradients \nabla f_i(\mathbf{x}^\star) \neq \mathbf{0} whenever examples disagree, continuously kicking constant-rate iterates off \mathbf{x}^\star.

SGD on the Valley: Transient Descent into a Noise Cloud

Valley Simulation

  • Adding independent unit-variance Gaussian noise \xi_1, \xi_2 \sim \mathcal{N}(0, 1) to the valley f(\mathbf{x}) = 0.1x_1^2 + 2x_2^2 (\lambda_1 = 0.2, \lambda_2 = 4.0) at \eta = 0.1 over 300 steps reveals two phases:
    1. Transient phase (steps 1–100): The deterministic gradient \|\nabla f(\mathbf{x})\| dominates \boldsymbol{\xi} and drives the iterate from (-5, -2) toward (0, 0).
    2. Stationary phase (steps 100+): Near the origin, \|\nabla f(\mathbf{x})\| shrinks to the noise level, trapping the iterate in an elongated cloud stretched along the flat axis x_1.
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, 0

step 300, x1: -0.371201, x2: -0.077858

Deriving the Stationary Variance of the Noise Ball

Second-Moment Recursion

  • 1D noisy evolution (x_{t+1} = (1 - \eta\lambda)x_t - \eta\xi_t): Squaring and taking expectations (\mathbb{E}[x_t\xi_t] = 0) separates deterministic contraction from noise injection: \mathbb{E}\big[x_{t+1}^2\big] = (1 - \eta\lambda)^2\,\mathbb{E}\big[x_t^2\big] + \eta^2 \sigma^2
  • Stationary balance (0 < \eta < 2/\lambda): Setting \mathbb{E}[x_{t+1}^2] = \mathbb{E}[x_t^2] = \mathbb{E}[x_\infty^2] yields the noise ball: \mathbb{E}\big[x_\infty^2\big] = \frac{\eta\,\sigma^2}{\lambda\,(2 - \eta\lambda)} \approx \frac{\eta\,\sigma^2}{2\lambda}

Three Laws Governing the Stationary Noise Ball

Geometric Interpretation

  • 1. Stability ceiling preserved: (2 - \eta\lambda) \to 0 as \eta \to 2/\lambda; variance explodes at the deterministic limit (\eta < 0.5 on the valley).
  • 2. Inverse curvature scaling (\propto 1/\lambda): Flat directions suffer both slower transient decay (\eta\lambda) and wider stationary fluctuations (16\times higher variance along \lambda_1 = 0.2 than \lambda_2 = 4.0).
  • 3. Linear \eta/b scaling: For \eta\lambda \ll 1, stationary variance scales as \frac{\eta\sigma^2}{2\lambda b}; halving \eta or doubling b halves the noise ball.

Simulating 10,000 Runs Confirms the Noise-Ball Formula

Empirical Verification

  • We simulate 10{,}000 independent SGD trajectories on the valley (\lambda_1 = 0.2, \lambda_2 = 4.0, \sigma^2 = 1) for 2{,}000 steps at \eta = 0.1 and \eta = 0.05:
    • At \eta = 0.1: measured variances match \frac{\eta}{\lambda(2-\eta\lambda)} = (0.2525, 0.0156) to within 1\%.
    • At \eta = 0.05: flat variance halves toward 0.1256, while steep variance drops slightly more than half (0.0156 \to 0.0069) because 2 - \eta\lambda_2 increases from 1.6 \to 1.8.
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]

Eliminating the Noise Floor: Robbins–Monro Conditions

Decaying Step Sizes

  • Robbins–Monro conditions (1951) for exact convergence \mathbf{x}_t \to \mathbf{x}^\star under noise: \sum_{t=1}^{\infty} \eta_t = \infty \qquad \text{and} \qquad \sum_{t=1}^{\infty} \eta_t^2 < \infty
  • Infinite travel budget (\sum \eta_t = \infty): Reaches \mathbf{x}^\star from any initialization (violated by geometric decay \eta_0\gamma^t, which freezes early).
  • Finite cumulative noise (\sum \eta_t^2 < \infty): Contracts tail variance to zero (achieved by \eta_t = \frac{c}{t+t_0} with O(1/t) MSE decay).

Minibatch Averaging and the 1/b Variance Reduction Law

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)

  • Per-example gradient covariance: Let \boldsymbol{\Sigma}(\mathbf{x}) = \mathbb{E}_i\big[(\nabla f_i(\mathbf{x}) - \nabla f(\mathbf{x}))(\nabla f_i(\mathbf{x}) - \nabla f(\mathbf{x}))^\top\big] with total variance \operatorname{tr}\boldsymbol{\Sigma} = \mathbb{E}_i\|\nabla f_i(\mathbf{x}) - \nabla f(\mathbf{x})\|^2.
  • Variance scales as 1/b: For b independent draws with replacement, the covariance divides by b: \mathbb{E}\big\|\mathbf{g}_t - \nabla f(\mathbf{x}_t)\big\|^2 = \frac{\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}_t)}{b}
  • The \eta / b noise-ball invariance: Substituting \sigma^2 \leftarrow \sigma^2 / b into the noise ball gives \mathbb{E}[x_\infty^2] \approx \frac{\eta}{b}\cdot\frac{\sigma^2}{2\lambda} for \eta\lambda \ll 1: doubling b and halving \eta produce the exact same stationary noise ball.

Stationary Excess Loss: Why Curvature Cancels Out

Where Training Stalls

  • Coordinate variance vs. loss curvature: Along eigenvector k (curvature \lambda_k, noise variance \sigma_k^2), stationary parameter variance is v_k \approx \frac{\eta\sigma_k^2}{2\lambda_k b}.
  • Exact \lambda_k cancellation: Multiplying by \frac{1}{2}\lambda_k in the quadratic loss cancels \lambda_k in every mode: \frac{1}{2}\lambda_k v_k = \frac{\eta\sigma_k^2}{4b}.
  • Curvature-free total excess loss: Summing over all d modes (\sum_k \sigma_k^2 = \operatorname{tr}\boldsymbol{\Sigma}) gives: \mathbb{E}\big[f(\mathbf{x}_\infty)\big] - f(\mathbf{x}^\star) \approx \frac{\eta\,\operatorname{tr}\boldsymbol{\Sigma}(\mathbf{x}^\star)}{4b}

Testing Excess Loss on the NASA Airfoil Dataset

Exact Data Measurements

  • On the standardized NASA airfoil self-noise dataset (n = 1{,}500 wind-tunnel examples, 5 features + 1 bias = 6 weights), linear regression with squared loss has an exact closed-form Hessian and minimizer \mathbf{w}^\star: \mathbf{H} = \frac{1}{n}\mathbf{X}^\top\mathbf{X}
  • The largest eigenvalue is L = \lambda_{\max}(\mathbf{H}) \approx 2.10, and evaluating all 1{,}500 per-example gradients \mathbf{g}_i = (\mathbf{x}_i^\top\mathbf{w}^\star - y_i)\mathbf{x}_i at \mathbf{w}^\star yields \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{w}^\star) \approx 3.24.
L = 2.10, tr Sigma = 3.24

Measured Excess Loss Matches η tr Σ / (4b) on Data

Theory vs. Experiment

  • At batch size b = 10 with \operatorname{tr}\boldsymbol{\Sigma}(\mathbf{w}^\star) = 3.24, our formula predicts:
    • At \eta = 0.05: \frac{0.05 \times 3.24}{4 \times 10} \approx 0.0041 above f(\mathbf{w}^\star) \approx 0.242.
    • At \eta = 0.025: \frac{0.025 \times 3.24}{4 \times 10} \approx 0.0020.
  • Averaging across 400 independent minibatch SGD runs over the third epoch confirms agreement within 2 \times 10^{-4}.
eta=0.05: excess loss 0.0042, predicted 0.0041
eta=0.025: excess loss 0.0021, predicted 0.0020

The Interpolating Regime vs. the Noise-Limited Regime

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

Sampling Without Replacement and Random Reshuffling

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:

  • Finite-population variance correction: Because the n per-example gradient errors sum to zero (\sum_{i=1}^n (\nabla f_i(\mathbf{x}) - \nabla f(\mathbf{x})) = \mathbf{0}), distinct examples in a minibatch are slightly negatively correlated (\mathbb{E}_{i \neq j}[\mathbf{e}_i^\top \mathbf{e}_j] = -\frac{\operatorname{tr}\boldsymbol{\Sigma}}{n-1}), scaling the variance by: \mathbb{E}\big\|\mathbf{g}_t - \nabla f(\mathbf{x}_t)\big\|^2 = \frac{\operatorname{tr}\boldsymbol{\Sigma}}{b}\cdot\frac{n - b}{n - 1}
  • Limiting checks: At b = 10, n = 1{,}500, the factor is 0.994 \approx 1; at b = n, the variance vanishes (0) as required for full-batch gradient descent.
  • Epoch-level cancellation: Across one full pass of n/b steps, random reshuffling forces the first-order gradient errors to sum to zero over the epoch, shrinking the stationary noise ball further at small \eta (Mishchenko et al., 2020).

Hardware Amortization: 100 Examples Cost the Same as 1

Throughput Benchmark

  • Every optimizer update pays fixed Python dispatch, autograd graph, and kernel-launch overhead plus weight memory reads independent of b.
  • On the 1{,}500-example airfoil dataset at fixed \eta / b = 0.005:
    • b = 1 (1{,}500 updates/epoch) and b = 100 (15 updates/epoch) both take comparable sub-millisecond time per update, making b = 100 50\text{--}100\times faster per epoch.
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

Sample Efficiency and Where Linear Scaling η ∝ b Breaks

Progress per Example

  • Why small-batch curves overlap: At fixed \eta / b = 0.005 and \eta\lambda \ll 1, b steps at rate \eta/b contract by (1 - \eta\lambda/b)^b \approx 1 - \eta\lambda, matching one step of size \eta per example processed.
  • Breakdown near the curvature ceiling 2/L:
    • At b = 100, \eta = 0.5 gives \eta L = 0.5 \times 2.10 = 1.05, inflating the noise-ball denominator (2 - \eta L)^{-1} and raising the last-epoch loss slightly (0.248 vs. 0.246).
    • Beyond b_{\max} = \frac{2/L}{0.005} \approx 190, linear scaling violates \eta < 2/L \approx 0.95 and diverges.

Concept Check: Noise Ball, Excess Loss, and Batch Size

Check Your Understanding

Calculate the outcome in each minibatch SGD scenario:

  1. Quadrupling the batch size: In the small-step regime (\eta\lambda \ll 1), if you increase the batch size from b = 16 to b = 64 while holding \eta fixed, by what factor does the stationary excess loss change? What if you also scale \eta by 4\times?
  2. Anisotropic noise ball vs. isotropic excess loss: On the valley (\lambda_1 = 0.2, \lambda_2 = 4.0) with equal gradient noise variance \sigma_1^2 = \sigma_2^2 = 1, what is the ratio of stationary coordinate variances \mathbb{E}[x_{\infty,1}^2] / \mathbb{E}[x_{\infty,2}^2] for small \eta, and what is the ratio of their contributions to the excess loss?
  3. Linear scaling ceiling: If a loss surface has maximum curvature L = 5.0 and you tune \eta = 0.02 at b = 8 (\eta/b = 0.0025), at what batch size b does linear scaling \eta = 0.0025\,b hit the deterministic stability limit 2/L?

Concept Check: Noise Ball, Excess Loss, and Batch Size (answers)

Check Your Understanding

Answers:

  1. Holding \eta fixed reduces the excess loss \frac{\eta\operatorname{tr}\boldsymbol{\Sigma}}{4b} by 4\times; scaling \eta by 4\times keeps \eta / b constant, leaving the excess loss unchanged (while reaching the floor in 4\times fewer updates).
  2. Coordinate variance scales as 1/\lambda, so \mathbb{E}[x_{\infty,1}^2]/\mathbb{E}[x_{\infty,2}^2] \approx 4.0 / 0.2 = 20\times wider along x_1; in the excess loss \frac{1}{2}\lambda_k \mathbb{E}[x_{\infty,k}^2] \approx \frac{\eta\sigma_k^2}{4b}, \lambda_k cancels and both axes contribute equally (1:1).
  3. The stability limit is 2/L = 2/5.0 = 0.4, which linear scaling reaches at b = 0.4 / 0.0025 = 160 examples.

Summary: Stochastic and Minibatch Gradient Descent

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)