3.2.2 · D4Training Deep Networks

Exercises — Mini-batch gradient descent

2,676 words12 min readBack to topic

Quick symbol refresher (all from the parent):

Recall What every symbol means (open if unsure)
  • ::: total number of training examples.
  • ::: the batch size — how many examples we average over per step.
  • ::: the network's parameters (the numbers we tune).
  • ::: the loss on one example ; is its per-example gradient.
  • ::: the average loss over the whole dataset.
  • ::: the mini-batch gradient estimate.
  • ::: the learning rate (step size).
  • ::: per-example gradient standard deviation (how "spread out" the are).
  • epoch ::: one full pass over the whole dataset.

Level 1 — Recognition

Recall Solution

WHAT we do: match each to its position on the batch-size spectrum.

  • (a) Stochastic Gradient Descent (SGD) — one example per step, noisiest. See Stochastic Gradient Descent.
  • (b) Batch (full) gradient descent — exact gradient, one step per epoch. See Gradient Descent.
  • (c) Mini-batch gradient descent — the practical sweet spot. WHY: the only thing that distinguishes these three named methods is the value of relative to .
Recall Solution

Steps per epoch . WHY the ceiling : if the last batch is a leftover of fewer than examples, we still take a step on it, so we round up. Here exactly, so no leftover — the answer is simply .


Level 2 — Application

Recall Solution

Full batches: full batches of 128. Those cover examples. Leftover: examples form one final (short) batch. Total updates: . WHY it matters: with drop_last=True you'd throw away those 80 examples and get updates; otherwise you keep them and get . Either way, it's 391 (or 390) updates vs 1 for full-batch — that's the whole point of mini-batching.

Recall Solution

WHAT tool and WHY: the parent derived . We use it because it directly tells us how spread shrinks with batch size.

  • :
  • : Noise dropped by factor . Compute rose by factor (we processed 4× more examples). WHY this is the lesson: the cost bought only less noise. That's the diminishing-returns face of .
Recall Solution

(a) True gradient . (b) . . (c) Average of the two batches — the true gradient exactly. WHY: each batch alone is "wrong" (0, then 2), yet these two batches together tile all four examples once each, so their average must equal the full mean. This is unbiasedness in miniature: individual steps mislead, the ensemble is honest.


Level 3 — Analysis

Recall Solution

WHAT/WHY: Team A's noise is . Team B wants . Solve . Cross-check with the law: to halve noise you must quadruple the batch, and indeed . ✓ The takeaway: halving noise always costs a larger batch, because noise and .

Figure — Mini-batch gradient descent
Recall Solution

No. Look at the figure: the thin blue line (per-step mini-batch loss) genuinely rises at several steps, but the thick orange line (running/epoch average) falls steadily. WHY steps can go up: each step uses , not the true gradient . Sometimes a batch's gradient points slightly wrong, and momentarily increases the true loss. Over many steps the noise cancels (unbiasedness) and the average descends. The correct diagnosis rule: judge progress by the running / epoch average, never a single noisy step.

Recall Solution

Noise ratio . So compute buys only a noise reduction. WHY not worth it (on noise alone): means gains grow only as the square root of cost — a classic diminishing return. Worse, the parent warns that losing the helpful mini-batch noise can hurt generalisation by steering you toward sharp minima — see Generalization and Flat Minima. Big batches usually pay off only when combined with LR scaling and warmup.


Level 4 — Synthesis

Recall Solution

The rule: if you multiply by , multiply by . Here , so . WHY it works: a bigger batch gives a lower-variance gradient ( smaller), so a bigger step is safe — you're more sure of the direction. See Learning Rate Scheduling. WHY warmup: at the very start is random and the loss surface is wild; a sudden can diverge. So we ramp up gradually over the first few epochs, then hold. Optimisers like Adam in Momentum and Adam further tame the transition.

Recall Solution

WHAT breaks: with sorted data and no shuffle, early batches are all cats, later batches all dogs. Each batch's gradient is then systematically pulled toward one class — it is not a random sample of the whole dataset. WHY the theory collapses: the parent's unbiasedness proof required each index in to be drawn uniformly at random, giving . A "cats-only" batch violates that; its expectation is the cat gradient, not the full-data gradient. The estimator becomes biased, and the model oscillates (learn cats, forget them while learning dogs). The fix: shuffle every epoch so batches are approximately i.i.d. and the unbiasedness argument holds again.

Recall Solution

(a) Updates: per epoch updates. per epoch updates. (b) Examples processed: both do 5 full epochs, so both see examples. (c) Same data seen, but took steps vs 5× more parameter updates for the same data, so the small-batch run typically makes more progress per example (more, noisier nudges), while the big-batch run makes fewer, cleaner nudges.


Level 5 — Mastery

Recall Solution

(a) Updates per epoch . Since , leftover , so updates per epoch. (b) Scaling factor (with a short warmup ramp — see Learning Rate Scheduling). (c) Total updates . (d) Why not : noise only falls as , so past a point extra batch buys almost no direction quality, updates-per-epoch collapse (fewer chances to adjust), and the vanishing gradient-noise can steer you into sharp minima that generalise worse (Generalization and Flat Minima). Moderate with scaled is the sweet spot.

Recall Solution

(a) Derivation (WHAT/WHY each step): Why the : scaling a random quantity by a constant scales its variance by ; here . Why the sum splits: independence makes the variance of a sum equal the sum of variances (cross-terms vanish). Taking : . (b) Prediction: want . Sanity: to shrink noise you pay examples — the square-law bites hard, which is exactly why nobody chases tiny gradient noise.

Recall Solution

Diagnosis: the batch grew but stayed the same. The gradient is now less noisy yet each step is the same tiny size — so you're taking small, confident steps far too cautiously, wasting the extra information. Fewer updates per epoch compounds the crawl. Fix (two parts):

  1. Scale the learning rate: per the linear scaling rule (use warmup so it doesn't blow up early).
  2. Add warmup + possibly momentum/Adam (Momentum and Adam) to keep the large- start stable. WHY it works: low-variance gradients justify larger steps; matching to restores per-epoch progress. Ignoring the scaling rule is the classic "big-batch training is slow" pitfall.

Recall

Recall One-line self-tests (hide answers, try first)
  • Steps per epoch, ? ::: .
  • Batch to make noise ? ::: .
  • Multiply by 8 — what to ? ::: multiply by (with warmup).
  • Loss rose one step — broken? ::: No; batch noise. Judge the running average.
  • Cost to halve gradient noise? ::: the batch size.

Connections