Training and optimization
Weight Averaging, EMA, SWA, and Checkpoint Ensembling
Compare exponential moving averages, stochastic weight averaging, checkpoint interpolation, output ensembling, and normalization-state recalibration.
By the end you can
- Distinguish parameter averaging from prediction ensembling
- Explain EMA and SWA as different trajectory-aggregation policies
- Identify when weight interpolation fails because checkpoints occupy incompatible regions
- Validate averaged models, normalization state, calibration, and serving cost
Comparison
Four ways to combine trained states
They differ in storage, inference cost, and assumptions about parameter space.
The third card is not hypothetical. Take a zero-shot CLIP model and the model it became after fine-tuning, and blend their weights. That is WiSE-FT, published in 2022 by Wortsman and colleagues. One sentence of their abstract carries the whole method: “We address this tension by introducing a simple and effective method for improving robustness while fine-tuning: ensembling the weights of the zero-shot and fine-tuned models (WiSE-FT).” On ImageNet and five derived distribution shifts, the blend beat prior work by 4 to 6 percentage points under shift. ImageNet accuracy rose 1.6 pp. Six further shifts gained 2 to 23 pp. Fine-tuning and inference cost nothing extra.
What makes that arithmetic legal is the lineage, not the operation. The two endpoints are one model before and after a single fine-tuning run, so their coordinates already agree. Every other card on this page is a variation on whether that condition holds. The rest of the lesson is about how to check it rather than assume it.
Exponential moving average
Maintain a recency-weighted parameter shadow during training.
- Cost: one extra parameter copy
- Inference: one model
- Assumption: nearby trajectory states
- Risk: stale auxiliary state
Stochastic weight averaging
Average selected later checkpoints, often across a broad schedule phase.
- Cost: running average
- Inference: one model
- Assumption: connected useful region
- Risk: BatchNorm recalibration
Checkpoint interpolation
Blend two or more parameter states with chosen coefficients.
- Cost: offline operation
- Inference: one model
- Assumption: compatible coordinates
- Risk: loss barrier
Prediction ensemble
Average or combine outputs from separate models.
- Cost: multiple forward passes
- Inference: expensive
- Assumption: complementary errors
- Risk: latency and maintenance
Key idea
An averaged model is a new model, not a free checkpoint upgrade
Averaging can change confidence, calibration, decision thresholds, and subgroup behavior. Even when accuracy improves, downstream policies may need retuning.
Run the same validation, robustness, latency, and safety checks used for any candidate. Store the averaging recipe so the artifact can be reproduced.
That obligation has been benchmarked rather than merely asserted. The first large-scale comparison of predictive-uncertainty methods under dataset shift ran at NeurIPS in 2019, across MNIST, CIFAR-10, ImageNet, a text task (20 Newsgroups), an ad-click task (Criteo) and a genomics task. Ovadia and colleagues report two findings that matter here. The repair everyone reaches for first is not enough: “traditional post-hoc calibration does indeed fall short”. What held up as the data moved was combining models. “Deep ensembles seem to perform the best across most metrics and be more robust to dataset shift.” The price is bounded — “We found that relatively small ensemble size (e.g. M = 5) may be sufficient” — so the comparison a team owes itself is five members against one averaged artifact, not an open-ended sweep. An independent group reached the same ordering against MC-dropout on computer-vision tasks in 2020, concluding that “Our comparison demonstrates that ensembling consistently provides more reliable and practically useful uncertainty estimates”.
Read against this lesson, that is a warning about accounting. Accuracy and calibration were measured separately in that benchmark because they moved separately. An averaged or ensembled artifact that matches the base checkpoint on accuracy has not been evaluated yet. Its confidence still has to be checked under the shift it will actually meet.
Parameter arithmetic creates a new evaluation obligation.
EMA follows the trajectory with a controllable memory horizon
An exponential moving average updates a shadow parameter vector after successful optimizer steps. A high decay coefficient changes slowly and emphasizes a longer history.
Averaging the weights, rather than the predictions, is what buys the gain. Mean Teacher is the canonical demonstration, at NIPS 2017. Tarvainen and Valpola state the design and the result together in their abstract: “To overcome this problem, we propose Mean Teacher, a method that averages model weights instead of label predictions. As an additional benefit, Mean Teacher improves test accuracy and enables training with fewer labels than Temporal Ensembling. Without changing the network architecture, Mean Teacher achieves an error rate of 4.35% on SVHN with 250 labels, outperforming Temporal Ensembling trained with 1000 labels.” The weight average beat the prediction average, and the prediction average had four times as many labels. With Residual Networks the same method improved CIFAR-10 with 4000 labels from 10.55% to 6.28%, and ImageNet 2012 with 10% of the labels from 35.24% to 9.11%.
The decay coefficient is a hyperparameter, not a default to inherit, and it is a nasty one. Karras and colleagues at NVIDIA open their 2024 paper with the practical problem: “Unfortunately, the EMA decay constant is a cumbersome hyperparameter to tune because the effects of small changes become apparent only when the training is nearly converged.” They then priced getting it wrong. Sweeping the EMA length of a single weight tensor, holding every other tensor at the global optimum, moved FID by as much as 10%, from 7.24 to about 6.5, in their CONFIG B. One tensor's memory horizon, and the headline metric moves. Their engineering answer is post-hoc EMA: store two averaged parameter vectors during the run, γ1 = 16.97 and γ2 = 6.94 — relative widths σrel of 0.05 and 0.10 — snapshotted once every 4096 training steps at batch size 2048, then reconstruct any EMA profile after training instead of guessing it before. The full paper improved the ImageNet-512 record FID from 2.41 to 1.81.
EMA can stabilize evaluation, especially under noisy updates, but it lags rapid adaptation. The update cadence must align with skipped steps, accumulation, and distributed synchronization. PyTorch pins that clock to optimizer updates explicitly: “For SWA and EMA, this call is usually done right after the optimizer step().”
EMA is optimizer-adjacent state whose clock must be defined precisely.
Example
Averages that look valid in code but fail as models
Parameter arithmetic assumes compatible coordinates and functions.
The permutation problem has since been measured, and partly solved. The Git Re-Basin algorithms, published in 2022, permute one model into the coordinates of a reference model. That produced “zero-barrier linear mode connectivity between independently trained ResNet models on CIFAR-10”. Averaging two independently trained networks needs that alignment done first. The same paper reports a counterexample to its own single-basin claim.
The barrier itself has a measurement and a published threshold. An ICML 2020 paper built instability analysis around a single quantity, defining “the error barrier height of p as the maximum increase in error from that of W1 and W2 along path p”, evaluated along the straight line between two networks. Frankle and colleagues found that the barrier is a property of when the two runs diverged, not of how good either endpoint is: “All but the smallest MNIST network are unstable to SGD noise at initialization according to linear interpolation. However, by a point early in training (3% for ResNet-20 on CIFAR-10 and 20% for ResNet-50 on ImageNet), all networks become stable to SGD noise.” Three percent of a ResNet-20 run on CIFAR-10 is the entire shared prefix it took. That gives the practice a test with a number in it. Before averaging two checkpoints, ask not whether both score well, but whether they share a training prefix. If the run history cannot answer that, measure the error along the line between them.
- Independent permutations: Two networks use hidden units in different orders, so direct weight averaging destroys features. Git Re-Basin exists to permute one model into the reference model's coordinates before the merge, not after it fails.
- Separated basins: Interpolation crosses a high-loss region even though both endpoints perform well. The diagnostic is the error barrier height along the straight path, and the empirical answer is that runs sharing a prefix past 3% of training for ResNet-20 on CIFAR-10, or 20% for ResNet-50 on ImageNet, land in a linearly connected region while independently initialised ones do not.
- Stale BatchNorm: Averaged weights use running statistics from one trajectory point and produce shifted activations, because those statistics were never collected for the averaged parameters. They have to be recomputed by an extra pass over the training data, shipped in PyTorch as the separate call torch.optim.swa_utils.update_bn().
- Mixed fine-tuning regimes: One checkpoint has frozen lower layers while another adapted them substantially. That is the opposite of the WiSE-FT case, where the two endpoints are one model before and after the same fine-tuning run and therefore still share coordinates.
- EMA clock bug: The shadow state advances on skipped overflow steps and drifts from actual optimizer updates, instead of being taken right after the optimizer step as the averaging utilities specify.
Analogy
Blending several drafts of the same map
Several drafts of one map, drawn on the same coordinate system, average into something better than any single draft. Small errors may cancel while shared structure remains.
Change the projection and the arithmetic stops meaning anything. Independently trained networks stand in exactly that relation, since permuted units and rescaled layers can describe the same function in incompatible coordinates.
The geometry under the metaphor was made measurable at NeurIPS in 2018. Garipov and colleagues state it: “We show that the optima of these complex loss functions are in fact connected by simple curves, over which training and test accuracy are nearly constant.” The caveat is inside the finding. The connecting paths exist, and they are curves. Weight averaging takes the chord instead, and a chord between two separately found optima can leave the region the curve stays inside. That is why the same result that establishes connectivity does not license straight-line interpolation between arbitrary models. Their curve-finding procedure also yields Fast Geometric Ensembling, which trains a competitive ensemble in the wall-clock time of a single model and outperformed Snapshot Ensembles on CIFAR-10, CIFAR-100 and ImageNet.
So the safe case is the narrow one the map picture describes: states drawn from one connected stretch of a single trajectory, where the chord and the curve are nearly the same line.
Weight averaging assumes comparable coordinates and a useful connected region.
Visual
A typical SWA workflow
SWA averages selected states rather than every early checkpoint.
The recalibration step is part of the method, not housekeeping bolted on afterwards. The SWA paper builds it in: “we run one additional pass over the data, as in Garipov et al. [2018], to compute the running mean and standard deviation of the activations for each layer of the network”. The reason is stated in the same sentence. Those statistics are not collected during training, because no training step ever ran with the averaged weights.
PyTorch ships that pass as its own API rather than folding it into the averaging call. “torch.optim.swa_utils.update_bn() is a utility function used to update SWA/EMA batch normalization statistics at the end of training”, and “update_bn() applies the swa_model to every element in the dataloader and computes the activation statistics for each batch normalization layer in the model”. It costs a full pass over the data and it must be invoked explicitly. Skip it and you deploy a model whose weights are an average and whose normalization buffers belong to one arbitrary point of the trajectory.
Reach a useful region
Train normally until the model has learned a competitive representation.
Explore with a chosen schedule
Use late-stage learning rates that visit nearby useful states.
Accumulate weight averages
Update the running mean at declared checkpoints.
Recompute normalization statistics
Pass training data through averaged weights when BatchNorm requires it.
Evaluate and calibrate
Treat the averaged model as a new candidate with its own evidence.
Visual
Evaluate an averaging strategy
Separate trajectory smoothing from ensemble diversity.
Two of these steps are usually underestimated. Step 3 is a data pass, not a flag: update_bn() runs the averaged model over the dataloader before any evaluation number is believable. Step 4 has a fixed candidate list because the five objects are genuinely different, and their published results do not transfer between them. WiSE-FT's 1.6 pp on ImageNet is an interpolation result. Mean Teacher's 4.35% error on SVHN with 250 labels is a weight-averaging result. The deep-ensemble advantage that survived dataset shift at NeurIPS 2019 is a prediction-ensemble result about calibration rather than accuracy. Quoting one of them to justify another is the most common way this comparison goes wrong.
Step 5 then prices what step 4 chose. An ensemble of about five members — the size the shift benchmark found sufficient — costs five forward passes at every request, forever. An average costs one.
1. Choose compatible states
Use checkpoints from one aligned trajectory or verify interpolation paths.
2. Define the clock
State which successful updates or checkpoints enter EMA or SWA.
3. Rebuild auxiliary state
Recompute BatchNorm statistics and preserve tokenizer or preprocessing versions.
4. Compare alternatives
Evaluate base checkpoint, EMA, SWA, interpolation, and output ensemble.
5. Price deployment
Include extra storage, latency, calibration, and maintenance.
Averaging can complement but not replace checkpoint selection
Averaging poor or incompatible states does not create a strong model automatically. The candidate set still needs a coherent training phase and validation rationale. Keep a simple strong checkpoint as a control. If averaging helps only after broad search, include that selection cost in the result.
Stochastic weight averaging was published at UAI in 2018. Averaging points along the SGD trajectory “leads to better generalization than conventional training”, its five authors write, and they describe SWA as “extremely easy to implement” with “almost no computational overhead”. Wortsman and colleagues carried the idea into fine-tuning at ICML in 2022. Their model soups averaged weights “without incurring any additional inference or memory costs”. The resulting ViT-G reached “90.94% top-1 accuracy on ImageNet”.
Notice what both claims are about: cost, measured against a trained model that already existed. Neither says averaging supplies quality that the candidate states did not have. The candidates in the SWA result come from one trajectory under a chosen schedule. The endpoints in the WiSE-FT result are a model and its own fine-tuned descendant. Reproduce the cheapness without reproducing the compatibility and there is nothing left of the finding.
Aggregation improves a trajectory only when the selected states already contain compatible useful behavior.
Key takeaways
- EMA, SWA, checkpoint interpolation, and prediction ensembling aggregate trained states through different mechanisms and costs, and their published results are not interchangeable: WiSE-FT's 1.6 pp on ImageNet is interpolation, Mean Teacher's 4.35% error on SVHN with 250 labels is weight averaging, and a deep ensemble's calibration advantage under shift is neither.
- EMA needs a precisely defined update clock, aligned with successful optimizer steps, accumulation, and distributed synchronization — PyTorch places the averaging call right after the optimizer step. The decay length is itself a tuned hyperparameter, worth up to a 10% FID swing from one tensor alone in the CONFIG B sweep by Karras and colleagues.
- SWA averages selected late trajectory states, and the SWA paper's own procedure includes one extra pass over the training data to recompute each layer's running mean and standard deviation, because those statistics were never collected for the averaged weights. PyTorch exposes it as torch.optim.swa_utils.update_bn() and does not call it for you.
- Direct parameter averaging assumes compatible coordinates. Git Re-Basin has to permute one network into the reference model's coordinates before a merge, and the loss barrier is testable via error barrier height along the straight path — it vanishes once two runs share a prefix past 3% of training for ResNet-20 on CIFAR-10, or 20% for ResNet-50 on ImageNet.
- An averaged artifact can change calibration, thresholds, subgroup behavior, and robustness, so it requires full candidate evaluation. The NeurIPS 2019 shift benchmark found post-hoc calibration falls short while deep ensembles stayed best across most metrics, with about M = 5 members sufficient.
- A strong unaveraged checkpoint remains an essential control: the SWA and model soups results are claims about cost against an existing trained model, so reproducing the cheap operation without the compatibility of the states reproduces nothing.