3.2.1 · D4Training Deep Networks

Exercises — Stochastic gradient descent (SGD)

3,106 words14 min readBack to topic

Reminder of the one rule everything rests on — the SGD update: Here is the parameter we tune, (eta) is the learning rate = how big a step we take, is the mini-batch gradient = the average slope measured on randomly chosen examples, and is the slope contributed by example .


Level 1 — Recognition

Recall Solution 1.1

Compare each batch size to .

  • (a) averages over all Full-batch GD.
  • (b) uses a single example → pure (online) SGD.
  • (c) uses of , so mini-batch SGD. What it looks like: (a) is one smooth expensive step, (b) is a wildly jittery cheap step, (c) is the practical middle.
Recall Solution 1.2

One epoch = one full sweep = updates. Over epochs: updates. Why: each update consumes one mini-batch of examples, and batches tile the whole dataset exactly once.

Recall Solution 1.3

(a) False. With uniform random sampling — it is unbiased. (b) True. Variance (with = per-example gradient variance from the definition above), so makes it = half. (c) False. Noise never vanishes with fixed ; bounces in a ball of radius .


Level 2 — Application

Recall Solution 2.1

Each step: .

  • Draw : ; .
  • Draw : ; .
  • Draw : ; . What the figure below shows: read it left-to-right along the number line. The two orange dots are the data points and ; the violet star at is the true minimum. The magenta dots rising up the page are in order, and the magenta arrows are the actual jumps. Notice arrow 1 nudges right toward , arrow 2 leaps far right toward (overshooting past ), and arrow 3 pulls back left toward . The dotted vertical lines drop each onto the number line so you can read its value. Despite the zig-zag, every after the first sits inside the range and the values hover around the true mean — that is SGD's noisy-but-centered behaviour made visible.
Figure — Stochastic gradient descent (SGD)
Recall Solution 2.2

Full gradient: . At : . Why this contrasts: full-batch aims straight at in one clean move; SGD wobbles because each step only sees one point. Both are heading to .

Recall Solution 2.3

Variance , std-dev .

  • : var , std .
  • : var , std .
  • : var , std . Why: multiplying by cut the std-dev only from to (about , i.e. ) — diminishing returns.

Level 3 — Analysis

Recall Solution 3.1

Bounce distance std-dev . So the bounce shrinks by a factor of 2 — while costing the compute per step. Why it matters: cost buys only less jitter; this is the diminishing-returns story quantified.

Recall Solution 3.2

First, the role of : it must satisfy so the denominator actually grows with the step count (if the rate never decays — that is just constant ; if the denominator eventually hits zero or flips sign and the rate blows up). Larger = faster decay = the rate cools down sooner. Crucially, only scales the sums by a constant — it changes how fast you converge, not whether the Robbins–Monro conditions hold. Write for large (the constant offset doesn't change large- behaviour).

  • — the harmonic series diverges . ✓ This buys: total travel distance is unbounded, so you can reach the minimum no matter how far.
  • — this is the p-series with , which converges (to -ish) . ✓ This buys: steps shrink fast enough that accumulated noise is finite and averages out, so you actually stop bouncing. Why these two, not one? Diverging alone could bounce forever; converging alone could stall before reaching the min. Together: reach it and settle. The decay of this schedule is the slowest decay that still makes converge — a knife-edge sweet spot; tunes the constant in front but cannot break either property. See Learning Rate Schedules.
Recall Solution 3.3

Near the minimum the true gradient , so the update is dominated by the noise part of , whose typical size is . With fixed this size never shrinks, so keeps taking noise-driven steps of constant scale — a permanent wiggle in a ball of radius . With a decaying , each noise kick gets smaller, and because the total remaining wiggle is finite and vanishing — converges.


Level 4 — Synthesis

Recall Solution 4.1

Scale factor . New learning rate: Intuition: a larger batch gives a less noisy gradient, so each step is more trustworthy — you can afford a bigger step. The linear rule targets keeping the total parameter movement per epoch roughly constant: with larger batches you take fewer updates per epoch, so each update must move farther to cover the same ground.

Recall Solution 4.2

Updates per epoch ; total over epochs .

  • : updates.
  • : updates. So the big-batch run takes fewer steps in the same epoch budget. Linear scaling from base with : Why together: fewer, bigger, more-confident steps at a proportionally larger learning rate cover comparable distance — the synthesis of update-count, batch size and learning rate.
Recall Solution 4.3

Flat minima are favoured by more gradient noise, which the network cannot "sit still" inside a narrow sharp basin.

  1. Decrease batch size . Noise variance grows as shrinks → more exploration → escapes sharp basins. (See Batch Size and Generalization.)
  2. Increase the learning rate (within stability). The effective noise injected per step scales with , so a larger widens the bouncing ball and pushes SGD out of narrow minima. (See Learning Rate Schedules.) Both raise the temperature of SGD's random walk, biasing it toward wide, flat, generalizing basins — the implicit-regularization story linked to the Bias-Variance Tradeoff.

Level 5 — Mastery

Recall Solution 5.1

Exact gradient of the averaged loss is . Update: Subtract from both sides — this reveals the error : So : the error shrinks (or grows) by a constant factor each step — a geometric sequence, . With , :

  • .
  • .
  • .
  • . Since , , so . What it looks like: smooth exponential glide to the minimum — no wobble, because full-batch has zero gradient noise here. All three regimes (governed by ):
  • (i.e. ): error shrinks each step → converges to . Within this, gives = a monotone glide from one side; gives = convergence that alternates sides (overshoots then corrects), spiraling in. At exactly , one-step jump straight to .
  • (i.e. or ): error magnitude is constant. = no movement at all; = = oscillates forever between and , never settling.
  • (i.e. or ): error grows each step → diverges, flying away from the minimum. This is the too-large-learning-rate blow-up. Why this matters: the exact same contraction factor governs stability of the learning rate on any quadratic — too large ( here) doesn't just slow you down, it explodes.
Recall Solution 5.2

In 5.1 the gradient is the exact averaged gradient , so the noise term is identically ; the update is a pure contraction and glides in. In SGD the estimate is , which equals the true gradient plus a mean-zero noise term whose typical size is (here caused by which point we happened to draw). That noise never disappears at fixed : near the true part but the noise part keeps kicking around a ball of radius . The responsible quantity is the gradient-estimate variance (equivalently its square root, the noise std-dev ); to kill the wobble you must send under Robbins–Monro, exactly as in Exercise 3.2. Full-batch has effectively zero here, so no schedule is needed — it just contracts to the point.

Recall Solution 5.3

Need . Take logs: . Smallest integer: . Check: . ✓ And : . So .


Recall One-line recaps (self-test)

Updates in one epoch ::: . Meaning of ::: per-example gradient variance = spread of individual gradients around the true gradient. Effect of on noise variance ::: halves it (). Robbins–Monro pair ::: and . Role of in ::: decay constant, must be ; tunes speed, not whether R–M holds. Linear scaling rule ::: multiply by multiply by . Fixed- SGD endpoint ::: a bouncing ball of radius , not a point. Error law of exact-gradient step on quadratic ::: ; converges iff .

Back to the parent: Stochastic gradient descent (SGD) · Hinglish: 3.2.01 Stochastic gradient descent (SGD) (Hinglish).