3.2.1 · D2Training Deep Networks

Visual walkthrough — Stochastic gradient descent (SGD)

1,719 words8 min readBack to topic

This page zooms in on Stochastic gradient descent (SGD). If "gradient" or "loss surface" is fuzzy, keep Gradient Descent open in a tab.


Step 1 — What is a gradient, as an arrow on a hill?

WHAT. Imagine the loss as the height of a landscape. The horizontal position is — the knobs of the network. The height is how wrong the network is. We want the lowest point.

WHY. Before we can talk about estimating a gradient, we need to see what the true one is. The gradient is nothing mystical: it is the little arrow that points straight uphill, and its length says how steep the slope is.

PICTURE. In the figure, the white curve is the loss. At one point sits a chalk-blue dot (our current ). The pink arrow points up-slope; SGD (and plain GD) walks the opposite way — that's the minus sign in the update.

Figure — Stochastic gradient descent (SGD)

Term by term, the true gradient is an average over the whole dataset:

Each example produces its own little arrow . The true gradient is their average.


Step 2 — Every example has its OWN arrow (they disagree!)

WHAT. Draw the per-example arrows at the same point . They scatter — some steep, some shallow, some pointing slightly left, some right.

WHY. This scatter is the whole reason SGD is "stochastic". Any one arrow is a bad guess for the true direction. But their average (the black arrow) is exactly . This is the picture we must keep in our head.

PICTURE. A fan of faint chalk arrows spraying out from the dot; the bold pale-yellow arrow through the middle is their average — the true gradient.

Figure — Stochastic gradient descent (SGD)

Step 3 — Grab a handful: the mini-batch estimator

WHAT. Pick examples uniformly at random — call this set (a mini-batch). Average only their arrows:

WHY. We can't afford all arrows every step (millions of them). Averaging a random handful is cheap. The question the rest of the page answers: does this handful-average point the right way?

PICTURE. From the same fan of arrows, three are circled in chalk-blue (the sampled batch). Their short average arrow sits near — but not exactly on — the true yellow arrow.

Figure — Stochastic gradient descent (SGD)

Step 4 — WHY the handful is unbiased (points right on average)

WHAT. "Unbiased" means: if you repeated the random draw many times and averaged all the resulting arrows, you'd land exactly on the true gradient. No systematic lean.

WHY expectation, not just averaging? We need a tool that says "the typical value over all possible random draws". That tool is the expectation — literally the probability-weighted average outcome. We use it because we can't try every batch, but we can reason about the average of all of them.

For a single random index , every example is equally likely (probability ):

Averaging such independent picks doesn't change the mean:

PICTURE. Many faint sampled-average arrows drawn on top of each other (many possible batches). Their own average — the bold pale-yellow arrow — sits exactly on the true gradient. Off by a bit each time, dead-on in the long run.

Figure — Stochastic gradient descent (SGD)

Step 5 — WHY the noise shrinks like

WHAT. Individual arrows scatter around the true one. How wide is that scatter? We measure it with variance — the average squared distance from the mean.

WHY variance, and why ? We need one number for "how far off a typical batch is". Variance is that number. The magic: averaging independent noisy arrows shrinks the scatter by a factor of . Let be the spread of a single example's arrow. Then

The out front gets squared to ; the independent variances add up to ; multiply and one survives in the denominator.

PICTURE. Two clouds of sampled arrows around the true gradient: a wide cloud for (chalk-pink) and a tight cloud for (chalk-blue). Same center, different width.

Figure — Stochastic gradient descent (SGD)

Step 6 — Edge case A: collapses to plain GD

WHAT. Put : the batch is the whole dataset. Then exactly, and the variance is as small as it can get.

WHY show it. A good formula must reproduce the extremes. At the noise cloud shrinks to a single point — that's Gradient Descent, the deterministic method. SGD contains GD as its zero-noise limit.

PICTURE. The scatter cloud collapsing to a dot as grows from toward ; the arrow becomes the exact yellow one.

Figure — Stochastic gradient descent (SGD)

Step 7 — Edge case B: constant never fully settles

WHAT. With a fixed learning rate , even at the minimum the noisy arrow is non-zero, so keeps twitching. It settles into a ball of wobble of radius , never a point.

WHY. The step is . At the bottom the true gradient is zero, but still carries noise of size . The pushes never stop. To actually land, shrink over time — the Robbins–Monro conditions (still able to travel far) and (noise eventually averages out). See Learning Rate Schedules.

PICTURE. Two paths into a bowl: constant- (pink) orbiting the minimum in a fuzzy ring; decaying- (blue) spiralling inward to the center.

Figure — Stochastic gradient descent (SGD)

The one-picture summary

Everything on one board: the cloud of per-example arrows (their center = true gradient), one sampled handful (the noisy estimate that's right on average), the shrinking cloud as grows, and the downhill walk that reaches the minimum despite the jitter.

Figure — Stochastic gradient descent (SGD)
Recall Feynman: the walkthrough in plain words

Picture a foggy valley. At your feet, every single training example whispers a slightly different "downhill this way" — a whole fan of arrows that disagree. If you listened to all of them and averaged, you'd get the perfect downhill direction, but that's exhausting to compute. So instead you listen to just a random handful. Their average is a bit off — it wobbles — but here's the beautiful part: it's off in a fair way. Over many steps the wobbles cancel, so you're marching in the right direction on average. Listen to more examples (bigger handful) and the wobble gets smaller, but four times the effort only halves the wobble — so a medium handful is the sweet spot. And if you keep your steps the same size forever, you'll circle the bottom forever; take smaller and smaller steps and you finally settle right in the pit.


Quick self-check

Why is one example's gradient a bad step direction but the average safe?
A single arrow is off-center (high variance), but the expectation of the random pick equals the true gradient — so averaging removes the bias.
To cut the gradient std-dev in half, by what factor must you grow the batch?
, because std-dev .
What happens to when ?
It equals the exact full gradient ; variance is minimized — SGD becomes plain GD.
Why does constant prevent exact convergence?
At the minimum the noisy estimate is still non-zero, so bounces in a ball of radius .