TL;DR
Stochastic gradient descent (SGD) is gradient descent where each step uses the gradient from one randomly chosen example, or from a small random batch, instead of the gradient over the whole dataset. Each step's direction is noisy, but it is correct on average, and it costs a tiny fraction of a full pass.
Almost every neural network is trained this way, because a full-dataset gradient is too expensive to compute before every update when the dataset has millions of examples.
Where you'll see it: in the training loop of nearly every deep learning model, and as the base step that optimisers such as momentum and Adam build on.
Intuition
Many cheap, slightly wrong steps
Suppose you are lost on a hillside and want to reach the valley. Gradient descent polls every data point for its opinion before each step. SGD asks one point chosen at random, follows its advice, and then asks another. Any single point may be misleading about the best direction, but the advice averages out, and you get to the bottom far sooner because you never wait for the full survey.
That is the trade. A full gradient is an accurate arrow that takes a long time to compute. A single-example gradient is a cheap arrow that jitters around the true one. For large datasets the cheap arrows win, because the time saved per step lets you take thousands of steps for the price of one exact step.
flowchart LR
A[Pick one example or a batch] --> B[Compute gradient on it]
B --> C[Step against that gradient]
C --> D{Converged or out of budget?}
D -- No --> A
D -- Yes --> E[Stop]
The math
The objective
Training usually minimises an average loss over a dataset of examples. Write for the loss on example with parameters . The objective is
Full gradient descent uses at every step. That sum touches all examples before the parameters move once.
The stochastic update
SGD picks an index uniformly at random from and updates
where is the learning rate at step . Each step touches one example, so it costs of a full gradient.
The reason this works is that the random gradient is an unbiased estimate of the true one. Because is uniform, the expected value of the step direction is
Each step is noisy, but on average it points where full gradient descent would.
Mini-batches
In practice SGD uses a batch of examples, chosen at random, and averages their gradients:
The estimate is still unbiased. If the per-example gradients have variance and are drawn independently (with replacement, for simplicity), the variance of the batch average is . Larger batches give steadier steps, at a higher cost per step.
Learning-rate schedules
A constant learning rate leaves the iterates bouncing around the minimum at a scale set by the noise. To converge exactly, the rate has to shrink. The classical Robbins-Monro conditions are
The first says the steps must be large enough to reach the minimum from anywhere. The second says the noise must average out. A rate such as satisfies both. Modern training usually uses a decaying or warm-up schedule instead, because it is easier to tune and reaches good solutions faster, even though it does not carry the same convergence guarantee.
Worked examples
Minimise for the data . The minimum is at the mean . Each example gives , with gradient . Start at with learning rate .
- At , the four per-example gradients are . Their average is , which is exactly the full gradient . So the average of the noisy gradients is correct.
- Suppose the random picks are , then , then , then .
- Step 1 uses : gradient , so .
- Step 2 uses : gradient , so .
- Step 3 uses : gradient , so .
- Step 4 uses : gradient , so .
Interpretation: after four steps the iterate is near 2.4, heading toward 4. The individual steps were pushed around by which example was picked, sometimes strongly (the step is large), but no step went the wrong way.
Use the same four points at , where the per-example gradients are . Their mean is , and their population variance is , computed from squared deviations divided by 4.
- Batch size : the variance of the gradient estimate is , so the standard deviation is about .
- Batch size , sampled with replacement: the variance is , standard deviation about .
- Batch size , sampled with replacement: the variance is , standard deviation about .
Interpretation: doubling the batch halves the variance of the direction but doubles the work per step. Going from one example to four cut the noise in half in standard-deviation terms, and that is the knob you tune when the training loss is too jittery.
A dataset has examples. Train with batches of .
- One full pass through the data is called an epoch. Each epoch needs steps.
- A single full-gradient step would touch all 1,000,000 examples. One epoch of SGD touches each example once, in 3,907 updates.
- Ten epochs is about 39,070 updates, each costing example-gradients, for about example-gradients in total. Full-batch gradient descent with the same number of updates would need .
Interpretation: SGD makes almost 40,000 moves in the time a full-batch method makes ten. That speed, not a better final answer, is usually why it wins on large data.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Huge dataset, cheap steps | Noise in each step is large with batch size 1 | Mini-batches of a few hundred, tuned to the hardware |
| Loss curve jitters and will not settle | Constant learning rate keeps the iterates bouncing | Decay the learning rate, or average the iterates |
| Ill-conditioned loss surface (steep in some directions, flat in others) | Plain SGD zig-zags and crawls along the flat directions | [[momentum |
| Training loss falls but validation loss rises | The model fits the noise in the sampled data | Regularise, stop early, or see Overfitting |
| Batches always drawn in the same order | Data ordering biases the updates | Shuffle every epoch |
The main failure mode is treating the learning rate as a detail. With a rate too large, SGD diverges, and the loss jumps to infinity or NaN. With one too small, progress is slow and the run looks stuck. Most of the tuning effort in deep learning goes into this one number and its schedule.
History
Herbert Robbins and Sutton Monro published their stochastic approximation method in 1951. It finds the root of a function when you can only observe noisy values of it, and the update they proposed is the SGD step with the decaying rate condition given above. Jack Kiefer and Jacob Wolfowitz followed in 1952 with a related method that estimated the gradient by finite differences.
For decades the method was a tool for statistics and control. Its use in learning grew when backpropagation made gradients of multilayer networks practical in the 1980s, and networks were then trained one example at a time. Mini-batches became the standard as training moved onto parallel hardware, because a batch of examples is processed efficiently in one matrix operation. Today's large models are trained with mini-batches and a learning-rate schedule, which is the same idea Robbins and Monro described.
Check yourself
QuestionRecall: why is a single-example gradient an unbiased estimate of the full gradient when the example is chosen uniformly at random?Show answer
Because the expected value of the gradient for a uniformly chosen index is the average of the per-example gradients over all examples, which is exactly . The average over the random choice reproduces the full gradient, even though any single draw is off.
QuestionUsing the four points from the first example, the full gradient at is . If you pick at random, what is the gradient, and how far is it from the true gradient?Show answer
The gradient is . It differs from the true by . That gap is the noise the SGD step carries, and it is why a single draw can point well away from the full-gradient direction.
QuestionWhy does the variance of a mini-batch gradient fall as the batch grows, and what does that cost?Show answer
If per-example gradients have variance and are independent, the average of of them has variance , so doubling halves the variance. The cost is that each step now needs example-gradients, so fewer steps fit in the same compute budget. This is the tradeoff between noise and speed.
QuestionApplied: a model trains on 50,000 examples with batch size 500. How many steps are in one epoch, and roughly how many steps in 20 epochs?Show answer
One epoch takes steps. Twenty epochs take steps. Each of those steps sees 500 examples, so the model has seen every example 20 times, once per epoch.