Skip to content
AI360XpertBeta
Core ML

Visual explainer

Stochastic Gradient Descent, Visually

Use one sampled gradient or average a batch. Follow four rows to see unbiased estimates, changing endpoints and residual noise.

Stochastic gradient descent updates weights from a sampled row or minibatch. It trades an exact empirical gradient for a cheaper estimate per update; individual updates can raise the full training loss.

Full-batch descent averages all four gradients at the same weight.
Full-batch descent averages all four gradients at the same weight.

Use a synthetic dataset with targets A = −1, B = +1, C = −1 and D = +1. The model predicts one scalar weight ww for every row. Each row has loss ℓi(w)=12(w−yi)2\ell_i(w)=\tfrac12(w-y_i)^2, so its gradient is gi=w−yig_i=w-y_i. The empirical mean is L(w)=12w2+12L(w)=\tfrac12w^2+\tfrac12, with gradient ww and minimum at zero.

At w=2w=2, the four arrows represent gradients 3, 1, 3 and 1 on one length scale. Full batch averages them to 2 before updating. It evaluates all rows at the same weight. This counts row evaluations, not elapsed time or hardware throughput.

Sample One Row

A single sampled row can produce either of two update endpoints.
A single sampled row can produce either of two update endpoints.

Subtract the sampled gradient times learning rate η=0.5\eta=0.5. From 2, choosing A gives 2−0.5(3)=0.52-0.5(3)=0.5; choosing B gives 2−0.5(1)=1.52-0.5(1)=1.5. The two lanes are alternatives from one starting state. Full batch would land at 1.

Uniform independent sampling at a fixed weight satisfies Ei[gi(w)]=∇L(w)\mathbb E_i[g_i(w)]=\nabla L(w). The expectation is over row selection and targets this finite dataset's objective. It does not automatically estimate an unknown population without further assumptions. See PyTorch's SGD update for the gradient subtraction convention.

Recompute After Every Step

Recomputing sampled gradients produces a path that does not simply halve.
Recomputing sampled gradients produces a path that does not simply halve.

For the illustrative draw order A, B, A, B, the recurrence is wt+1=0.5wt+0.5yiw_{t+1}=0.5w_t+0.5y_i. The weights become 2→0.5→0.75→−0.125→0.43752\to0.5\to0.75\to-0.125\to0.4375. Each point uses the new weight to compute the next gradient. The horizontal axis is update order; the letters identify the row consumed. This is one possible path, not the expected trajectory. Straight joins connect discrete states.

Average Two Independent Draws

Averaging two independent draws concentrates the gradient distribution.
Averaging two independent draws concentrates the gradient distribution.

At the fixed weight 2, one uniform draw has gradient 1 or 3 with equal probability. Two independent draws with replacement produce means 1, 2 or 3 with probabilities 0.25, 0.5 and 0.25. Both panels share gradient spacing and probability height; circles mark single gradients and diamonds mark averages. The single-draw variance is 1; the average's variance is 0.5.

For independent draws, averaging BB gradients gives variance σ2/B\sigma^2/B per coordinate. Sampling without replacement has a finite-population correction. Correlated examples and changing weights need separate analysis; a minibatch is not perfectly noise-free.

Row Improvement Can Mislead

A sampled row loss can fall while the full empirical loss rises.
A sampled row loss can fall while the full empirical loss rises.

Reset to the empirical optimum w=0w=0 and sample B. Its gradient is −1, so the update lands at 0.5. B's own loss falls from 0.5 to 0.125, yet the full mean loss rises from 0.5 to 0.625. Both bar panels start at zero and use the same loss scale; hollow bars are before and solid bars after. A useful local estimate is not a guarantee that each full-objective evaluation decreases.

Change The Learning Rate

Halving the learning rate halves a sampled step from the same state.
Halving the learning rate halves a sampled step from the same state.

Hold the weight at zero and the B gradient at −1. Rates 0.5 and 0.25 move to 0.5 and 0.25 on the shared weight ruler. Shrinking a rate reduces this update's displacement; it does not establish convergence by itself. Training schedules also depend on the objective and sampling process.

Sampling Must Match The Objective

Biased sampling moves the expected target away from the empirical mean.
Biased sampling moves the expected target away from the empirical mean.

Keep the four-row empirical objective fixed, but now select a +1 target with probability 0.75 and a −1 target with probability 0.25. The lower rulers show mean targets: a white dot at zero for uniform sampling, an amber triangle at +0.5 for biased sampling. Both rulers span −1 to +1, with a central zero tick.

The expected gradient becomes w−0.5w-0.5, so its zero is 0.5 rather than the empirical optimum zero. Unweighted nonuniform sampling changes the effective objective. Appropriate importance weights can correct it; random selection alone does not imply an unbiased estimate of the intended loss.

Count Work Per Pass

Four row evaluations allow four, two or one updates as batch size grows.
Four row evaluations allow four, two or one updates as batch size grows.

Each hollow circle is one row evaluation; each mint diamond is an update after its bracketed group. One four-row pass gives four updates at batch size 1, two at size 2 and one at size 4. Larger groups may use parallel hardware more effectively, but these counts do not predict wall-clock speed or which final model wins.

Row Order Changes The Endpoint

Reordering the same two rows changes the resulting weights.
Reordering the same two rows changes the resulting weights.

Restart at 2 with rate 0.5. A then B gives 2→0.5→0.752\to0.5\to0.75; B then A gives 2→1.5→0.252\to1.5\to0.25. The solid circles and dashed diamonds share update columns and weight units. Both paths use the same two rows, but later gradients are evaluated at different weights. Unbiased estimates at a fixed state do not make one evolving epoch exactly equivalent to a full-batch step.

Where It Breaks

Fixed-rate SGD can retain nonzero spread even when its mean is optimal.
Fixed-rate SGD can retain nonzero spread even when its mean is optimal.

Return to independent uniform draws, rate 0.5 and starting weight zero. The mean weight stays zero, but the expected squared weight qt=E[wt2]q_t=\mathbb E[w_t^2] follows qt+1=0.25qt+0.25q_{t+1}=0.25q_t+0.25. The zero-based plot shows 0,0.25,0.3125,0.328125,0.332031250,0.25,0.3125,0.328125,0.33203125, approaching the dashed limit 1/31/3. It plots an expectation over possible paths, not one training run.

Thus expected excess empirical loss approaches 12qt=1/6\tfrac12q_t=1/6. The remaining half unit of total loss is unavoidable disagreement among the targets. Persistent sample gradients at the optimum prevent exact settling in this example. This does not mean every constant-rate SGD problem has residual noise: if all individual gradients vanish at the same optimum, this mechanism disappears. Scheduling or larger batches can reduce noise, subject to their own conditions.

The Quick Version

  • Full batch averages gradients evaluated at one weight.
  • One sampled row chooses one possible endpoint.
  • Recompute gradients after every weight change.
  • Independent batch averages reduce gradient variance.
  • Sample loss can improve while mean loss rises.
  • A smaller rate shortens the same sampled step.
  • Biased sampling changes the effective objective.
  • Batch size controls updates per row-evaluation budget.
  • Row order changes the final weight.
  • Fixed-rate SGD can retain nonzero variance.