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.
Use a synthetic dataset with targets A = −1, B = +1, C = −1 and D = +1. The model predicts one scalar weight for every row. Each row has loss , so its gradient is . The empirical mean is , with gradient and minimum at zero.
At , 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
Subtract the sampled gradient times learning rate . From 2, choosing A gives ; choosing B gives . The two lanes are alternatives from one starting state. Full batch would land at 1.
Uniform independent sampling at a fixed weight satisfies . 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
For the illustrative draw order A, B, A, B, the recurrence is . The weights become . 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
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 gradients gives variance 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
Reset to the empirical optimum 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
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
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 , 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
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
Restart at 2 with rate 0.5. A then B gives ; B then A gives . 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
Return to independent uniform draws, rate 0.5 and starting weight zero. The mean weight stays zero, but the expected squared weight follows . The zero-based plot shows , approaching the dashed limit . It plots an expectation over possible paths, not one training run.
Thus expected excess empirical loss approaches . 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.