Stochastic Weight Averaging
Instead of using only the final set of weights training happens to land on, averaging together the weights from several recent points in training often lands in a wider, better-generalizing valley for free.
Prerequisites: Stochastic Gradient Descent, Cosine Annealing and Warm Restarts
Training with stochastic gradient descent doesn't settle smoothly into one exact point — the weights keep bouncing around near the bottom of the loss surface from batch to batch, even late in training, because each mini-batch's gradient is a noisy estimate. If you just grab whatever weights happen to be current when you decide to stop, you've picked one particular point out of that bouncing cloud, somewhat arbitrarily. Stochastic Weight Averaging (SWA) asks a different question: what if, instead of picking one point from that late-training cloud, you averaged several of them together?
The analogy: many blurry photos versus one sharp guess
If you're trying to pin down exactly where a hovering drone is, and your camera is a little shaky, one single photo might catch it slightly to the left or slightly to the right of its true position — that's the noise. But if you take ten photos over a few seconds and average their positions, the shake tends to cancel out because it wobbles in different directions each time, and the averaged position is closer to the drone's true, steady hovering point than any single shaky photo. SWA treats the late-training weight snapshots the same way: each one is a noisy read of "where the good region of the loss surface is," and averaging several of them cancels out some of that noise.
Averaging, and why it lands somewhere flatter
Concretely, SWA runs ordinary training as normal, then in a final phase — often with a constant or cyclical learning rate rather than one that's still decaying to near zero — it periodically snapshots the current weights and maintains a running average:
In words: the new running average () is the old running average () weighted by how many snapshots have gone into it so far (), blended with the newest snapshot (), then renormalized. Each new snapshot pulls the average only slightly, so it settles toward a stable central point among the recent snapshots rather than any one of them individually.
Because the loss surface near a good minimum tends to be curved in some directions and much flatter in others, and different noisy SGD steps wander in different directions within that flat region, averaging several nearby points tends to land nearer the center of that flat region rather than out toward one of its steeper edges — which is exactly the kind of flat minimum associated with better generalization.
Worked example 1: averaging three snapshots
Suppose late in training, three snapshots of a single weight are recorded: , , . The running average updates as: ; ; . The final averaged weight, , sits centrally among the three noisy snapshots rather than matching any single one — and none of the three individual snapshots is guaranteed to be the "best" one on its own.
Worked example 2: why the average can beat every individual snapshot
Suppose test accuracy for snapshots 1, 2, and 3 individually were , , and — each a noisy read of the model's true quality. Averaging the weights (not the accuracies) into and then evaluating that single averaged model on the test set often produces something like — better than any individual snapshot. This isn't guaranteed by the arithmetic (averaging weights is not the same as averaging predictions), but empirically it happens often enough that SWA is a reliable, nearly-free accuracy boost, because it exploits the specific way SGD's noise wanders around a flat basin rather than any single noisy readout of it.
What this means in practice
SWA is nearly free: it costs one extra set of weights to store and average, and no extra gradient computations, since it just re-uses snapshots from training you were already doing. It's commonly paired with a cyclical or constant learning rate at the end of training specifically so the snapshots explore the basin rather than converge to a single point too tightly to have anything interesting to average. It's distinct from SWA-Gaussian (SWAG), a related technique that also models the spread of those snapshots for uncertainty estimation, not just their average.
Averaging weights from several points late in SGD training, rather than using just the final point, tends to land in a flatter, better-generalizing region of the loss surface, at essentially no extra computational cost.
Practice
- Four snapshots of a weight are . Compute the running average after all four.
- Why does SWA usually pair with a constant or cyclical learning rate rather than one that's still decaying toward zero?
- Why is averaging weights different from averaging the predictions of several models (an ensemble)?
Averaging weights only works cleanly when the snapshots come from the same basin of the loss surface with compatible architectures and, for layers like batch normalization, requires recomputing running statistics afterward — batch norm's internal averages don't get fixed up automatically just because the weights were averaged. Naively averaging weights from very different training runs, or forgetting the batch-norm recalibration step, can produce a model that performs worse than any individual snapshot.
Practice in interviews
Further reading
- Izmailov et al., Averaging Weights Leads to Wider Optima and Better Generalization (2018)