5.6.10 · D3Machine Learning (Aerospace Applications)

Worked examples — Batch, mini-batch, stochastic gradient descent

3,368 words15 min readBack to topic

This page is the drill floor for the parent topic. We do not re-explain the theory here — we hit every case the topic can throw at you, one worked example per cell. If a symbol feels unfamiliar, jump back to the parent, then return.

Before anything, a one-line reminder of the machine we are cranking:


The scenario matrix

Every problem this topic can pose falls into one of these cells. Each row is a thing that can vary; the examples below are labelled with the cell they cover.

# Case class What makes it tricky Example
C1 Sign of the gradient — positive step must go down in , not up Ex 1
C2 Sign of the gradient — negative step goes up in Ex 2
C3 Zero gradient (degenerate) already at a flat spot — no move Ex 3
C4 Batch vs SGD on same data show noise vs exactness numerically Ex 4
C5 Variance shrinks as prove the noise-reduction number Ex 5
C6 Epoch bookkeeping, not divisible by leftover last batch Ex 6
C7 Limiting behaviour too big divergence / overshoot Ex 7
C8 Real-world word problem (aerospace) translate wall-clock into method choice Ex 8
C9 Exam twist — linear-scaling rule scale with correctly Ex 9
C10 Escaping a saddle (why noise helps) zero gradient that noise breaks Ex 10

The running toy model for the numeric examples is the simplest thing that still has a gradient:

The middle formula comes from the chain rule: differentiate and the inner falls out. We use it on every example below. Throughout, we keep the subscript on so it is always clear we mean one sample.


C1 — Positive gradient: step must go down in

Forecast: we start above the target. Guess — will increase or decrease after one step?

  1. Compute the gradient. . Because this is SGD (), the estimate equals this exact per-sample gradient: . Why this step? This is the only quantity the update rule needs; its sign decides direction.
  2. Read the sign. . A positive gradient means "loss rises as rises," so we must move down. Why this step? The rule subtracts a positive number → shrinks. Sign of = direction, automatically.
  3. Update. . Why this step? We took the general rule and plugged in.

Verify: we predicted would drop toward . Distance to the target went from down to , so we moved closer. ✓ Positive gradient ⇒ downward step. ✓

Figure — Batch, mini-batch, stochastic gradient descent

C2 — Negative gradient: step goes up in

Forecast: below target this time. Up or down?

  1. Gradient: . Again SGD, so . Why this step? Same formula; only changed.
  2. Sign: . Subtracting a negative number adds, so increases. Why this step? — the sign flip is the whole mechanism.
  3. Update: .

Verify: distance to went . Closer. ✓ Negative gradient ⇒ upward step. ✓ C1 and C2 together show the sign of always steers us toward the minimum, from either side.


C3 — Zero gradient: the degenerate "already there" case

Forecast: we are at . What should a good algorithm do? (Hint: nothing.)

  1. Gradient: , so . Why this step? At the minimum the prediction equals the target ; the residual is zero, killing the whole gradient.
  2. Update: . No movement. Why this step? step size is regardless of .

Verify: loss , already minimal; the algorithm correctly stops moving. ✓ This is the fixed point of the update: is stable iff . (Careful: this same condition holds at saddle points too — see C10.)


C4 — Batch vs SGD on identical data (noise vs exactness)

Forecast: which step is bigger — Batch, or the worst-case single-sample SGD step?

  1. Per-sample gradients: , . Why this step? We need each one to both average (Batch) and pick (SGD).
  2. Batch estimate: — here either single ; it is their average over , the exact . Update: . Why this step? Batch uses the mean over all samples.
  3. SGD estimate, sample 1: . SGD, sample 2: . Why this step? SGD () picks one random sample, so equals that sample's exact gradient — producing or depending on luck. That luck is the "noise."

Verify: average of the two SGD outcomes the Batch outcome. ✓ This is "SGD is unbiased" made numeric: on average SGD equals Batch, but any single SGD step (0.8 or 0.1) scatters around it. That scatter is the noise.

Figure — Batch, mini-batch, stochastic gradient descent

C5 — Variance really does shrink like

Forecast: going from to — does noise drop by or by ?

  1. Apply the formula (see Bias-Variance Tradeoff). Why this step? is an average of i.i.d. gradients; the variance of a mean of terms is .
  2. Plug in: ; ; .
  3. The catch — standard deviation. The noise you feel is the standard deviation : . From the std drops , i.e. a factor of , not . Why this step? This is the "diminishing returns" fact from the parent: noise falls as , so doubling helps ever less.

Verify: , , ; and . Ratio . ✓ Cell C5 confirmed.


C6 — Epoch bookkeeping when is NOT divisible by

Forecast: — but you can't do a third of an update. Round which way?

  1. Full batches: full batches of 300 = 900 samples. Why this step? Each update consumes exactly fresh samples.
  2. Leftover: samples remain — one partial last batch. Why this step? We must cover all data once to finish the epoch, so the remainder still gets its own update.
  3. Total updates: updates per epoch (3 full + 1 partial). Why this step? Ceiling, because the remainder still triggers one update.

Verify: ✓, and ✓. Contrast: Batch GD = 1 update/epoch, pure SGD = 1000 updates/epoch. Mini-batch sits between with 4.


C7 — Limiting behaviour: learning rate too large ⇒ divergence

Forecast: big = big steps. Does settle at , or fly past it forever?

  1. Step 1 gradient: . Update: . Why this step? We overshot the target and landed on the other side ().
  2. Step 2 gradient at : . Update: . Why this step? Now positive gradient pushes back — but by the same magnitude, straight back to .
  3. Diagnose: ping-pongs It never converges; loss stays high forever.

Verify: at , ; at , equal, no progress. ✓ For this quadratic the stability boundary is ; we sat exactly on it and got a perpetual oscillation. Any larger and it would blow up. This is why schedules and methods like Momentum and Adam exist.

Figure — Batch, mini-batch, stochastic gradient descent

C8 — Real-world aerospace word problem

Forecast: all three touch the same 2M samples per epoch. So which one updates the most within that pass?

  1. Compute cost per epoch is the same for all: touching all = 2M samples once seconds. Well inside 30 s. Good — the differentiator is update count, not total compute. Why this step? Number of gradients evaluated per epoch is for every method; only how you group them into updates (via ) changes.
  2. Batch GD: groups all 2M into 1 update. You improve once every 2 seconds of compute. Why this step? .
  3. SGD: tiny updates — extreme but very jittery and cache-unfriendly.
  4. Mini-batch : updates per epoch — many good-enough steps, GPU-friendly. This is the practical winner. Why this step? , ceiling (last batch has samples).

Verify: s ✓ (< 30 s, so all feasible on compute). , remainder , so ✓. Batch=1, SGD=2,000,000, mini-batch=7813 updates/epoch. Mini-batch gives thousands of trustworthy steps per pass — the 80/20 sweet spot.


C9 — Exam twist: the linear-scaling rule for

Forecast: batch got bigger. Multiply by , or by ?

  1. State the rule. Linear scaling: , so . Why this step? Larger ⇒ smaller gradient variance ⇒ the step is more trustworthy, so you may take a proportionally bolder step.
  2. Compute the ratio: .
  3. Scale: . Why this step? Direct application of the proportionality.

Verify: ✓ and ✓. (Sanity: it's the linear rule, so factor , not . In practice you also add a warm-up — see Learning Rate and Schedules — but the exam answer is .)


C10 — Why a little noise escapes a saddle

Forecast: at the gradient is... compute it. Will Batch GD ever move?

  1. Batch gradient at 0: . Update: . Stuck forever. Why this step? Exact gradient is exactly zero at the flat spot — Batch GD has no reason to move, even though is not a real minimum (loss keeps dropping for ).
  2. SGD's noisy estimate. Recall (from the definitions) SGD's estimate is , where is the sampling noise: mean zero, variance , coming from which random sample was drawn. At the mean part is , but a single draw of is generally nonzero — suppose this draw gives , so . Update: . Why this step? Because but , individual steps are nonzero even when the true gradient vanishes. That nonzero kick is precisely what nudges off the flat spot — impossible for noiseless Batch GD.
  3. Now the true gradient reappears and carries us downhill. At : , so even a noiseless step now continues , sliding to more-negative where (the loss) keeps decreasing. Why this step? Once the noise has moved us off the exact saddle, the gradient is no longer zero, so ordinary descent takes over and we keep making real progress. The noise only had to break the tie once.

Verify: (Batch frozen at the saddle) ✓; one noisy kick ✓; then , giving next , which is more negative than ✓ — i.e. still moving downhill. Conclusion: SGD's zero-mean sampling noise dislodges the parameter from a saddle where Batch GD would sit frozen forever; after one kick, deterministic descent finishes the job. This is the parent's "a little noise is good" made concrete. See Saddle Points and Non-Convex Optimization.


Recall Quick self-test on the matrix

Which cell(s) show that the sign of alone decides step direction? ::: C1 (positive) and C2 (negative). In C7, what exact sits on the oscillation boundary for ? ::: . In C6, why is the answer 4 and not 3 updates per epoch? ::: The 100 leftover samples form a partial final batch, so we take the ceiling . In C5, going cuts the std of noise by what factor? ::: (variance by 9, std by 3). When do and coincide, and when do they differ? ::: They coincide for SGD (, average of one term); they differ for mini-batch and Batch, where averages several . In C10, why does Batch GD freeze at but SGD does not? ::: The exact gradient there is 0, so Batch never moves; SGD's zero-mean noise gives a nonzero kick that escapes the flat spot.