3.2.2 · D2Training Deep Networks

Visual walkthrough — Mini-batch gradient descent

1,749 words8 min readBack to topic

We are going to earn every symbol before we use it. Let's start with the most basic object of all: a single training example's "which way to nudge" arrow.


Step 1 — What a "gradient" even is, as an arrow

WHAT. Take one training photo with its correct answer . The network makes a guess; the guess is a bit wrong; the loss measures how wrong. The gradient is an arrow that points in the direction of parameters that would make the loss bigger — so we step opposite to it.

WHY an arrow. Every network parameter is one axis you can slide. If there are two parameters, "parameter space" is a flat sheet, and the gradient is a genuine arrow on that sheet. We draw the 2-parameter cartoon because your eye can hold two dimensions; the real network has millions of axes, but the logic is identical per axis.

PICTURE. Each dot is one example's gradient arrow . They scatter — some point up-right, some down-left. No single arrow is "the truth"; they disagree.

Figure — Mini-batch gradient descent

Step 2 — The true gradient is the center of the cloud

WHAT. The quantity we actually want is the average of all arrows:

WHY an average. The whole-dataset loss is itself an average of the per-example losses. Slope-of-an-average = average-of-slopes, so the true gradient is literally the arithmetic mean arrow — the balance point of the whole cloud.

PICTURE. The amber arrow is : drop a pin so the cloud balances on it. It sits in the middle even though not one single dot lands exactly there.

Figure — Mini-batch gradient descent

Step 3 — A mini-batch is a random handful; its mean is our guess

WHAT. Pick arrows at random (call this set , the batch) and average just those:

WHY random. If we always grabbed the same handful, our guess would be permanently tilted toward that handful's quirks. Grabbing uniformly at random makes every arrow equally likely to be picked — that fairness is what makes the guess honest (Step 4).

PICTURE. Highlight dots (cyan ring), average only those (cyan arrow). Notice it lands near the amber true arrow but not on it. That gap is the whole story of the rest of the page.

Figure — Mini-batch gradient descent

Step 4 — On average, the guess is exactly right (unbiased)

WHAT. Repeat "grab a random handful, take its mean" many times and average those means. You get back exactly:

WHY it holds. (say "expected value") means "the long-run average over all the ways you could have drawn the batch." Each single arrow, drawn uniformly, has long-run average (every arrow gets picked equally often). Averaging things that each average to still averages to . So there is no systematic tilt — the guess is unbiased.

PICTURE. Many faint cyan batch-mean arrows fan out around the amber true arrow. They lean left and right, but their own center-of-mass sits dead on .

Figure — Mini-batch gradient descent

Step 5 — How wide is the wobble? Averaging shrinks spread

WHAT. The spread of the individual arrows is the variance (its square root is the typical distance of one arrow from the center). The spread of the batch mean is smaller:

WHY each move.

  • pulls out of the variance as — because variance measures squared spread, and scaling an arrow by scales its spread-squared by .
  • The sum splits into a plain sum of the pieces' variances — this step requires independence (Step 7 is the warning about when it fails). Independent wobbles partly cancel instead of piling up.
  • There are identical pieces each of size , giving . Multiply by .

PICTURE. Two clouds of batch-mean arrows: a fat one for small , a tight one for large . Same center, different fatness.

Figure — Mini-batch gradient descent

Step 6 — Why means diminishing returns

WHAT. Take the square root of the variance to get the wobble in the same units as the arrows: . This falls with , but slowly.

WHY square-root and not . People instinctively think "double the batch, halve the noise." Not so: the variance falls as , but the thing you feel — the standard deviation (typical distance) — falls only as . To halve the wobble you must quadruple the batch (and its compute cost).

PICTURE. Plot wobble against . It's a steep drop at first then a long flat tail — the tail is where extra compute buys you almost nothing.

Figure — Mini-batch gradient descent

Step 7 — The degenerate cases (never hit a scenario we didn't show)

WHAT & WHY & PICTURE, all four corners of the spectrum:

  1. (full batch). The "handful" is everyone, so exactly, wobble . Smooth path, but one arrow costs a whole dataset. This is plain Gradient Descent.
  2. (one example). Wobble — maximal. Very jittery, but each step is dirt cheap and the jitter can bounce you out of bad minima. This is Stochastic Gradient Descent.
  3. Not independent (sorted / unshuffled data). Step 5's split of the sum assumed independence. If every batch is all-cats-then-all-dogs, the arrows inside a batch are correlated, the cancellation fails, and is biased toward whatever class you're on. Fix: shuffle every epoch.
  4. Last leftover batch. With you get steps; the final batch holds only examples — a smaller handful, hence noisier, but still unbiased. Keep it or drop it.
Figure — Mini-batch gradient descent
Recall Quick self-check on the edge cases
  • Wobble at ? ::: essentially zero ().
  • Why does shuffling matter? ::: it restores the independence that Step 5 needs, keeping the estimate unbiased and low-variance.
  • Steps per epoch, ? ::: (last batch = 80 examples).

The one-picture summary

Everything above in a single diagram: a scattered cloud of per-example arrows (), their balance point (, amber), one random handful and its mean (, cyan), the fan of possible batch-means centered on (unbiased, Step 4), and a shrink-arrow showing that fan tightening as grows by (Step 5–6).

Figure — Mini-batch gradient descent
Recall Feynman retelling of the whole walkthrough

You want to know which way the whole class is leaning on a question, so you could ask all 5000 kids — but that's slow (Step 2, the true gradient ). Instead you ask a random handful of 30 and average their opinions (Step 3, ). Any one handful might lean a bit wrong, but if you grabbed handfuls fairly at random, over many handfuls their answers center exactly on the true class opinion — no built-in tilt (Step 4, unbiased). And the more kids per handful, the less each handful wobbles — but sneakily, to cut the wobble in half you need four times as many kids, not twice (Steps 5–6, the law). This all breaks if you keep asking the same clique or ask kids grouped by neighbourhood, because their answers then move together instead of cancelling — so you reshuffle who's in each handful every round (Step 7). That is mini-batch gradient descent, seen entirely as a cloud of arrows and their balance point.


Connections