Practical overview
When it fits
Use it when a PyTorch training workflow needs a maintained moving-average model alongside the live network, including workflows that require configurable update timing or nested teacher-output routing.
Who it is for
- PyTorch developers building custom model-training pipelines
- Researchers comparing live and EMA model weights during evaluation
- Teams implementing self-supervised or teacher-student training with nested modules
Documented uses
- Maintain an EMA copy of a PyTorch model
- Control EMA decay, warm-up and update frequency
- Record multiple EMA checkpoint streams and synthesize another EMA model
- Route target-module outputs into selected online submodules
Requirements & limits
The repository is public and MIT-licensed. Installation is documented with `pip install ema-pytorch`; the examples use Python and PyTorch code run in the developer's own environment.
- It is a library integrated into an existing PyTorch loop, not a hosted training application.
- The standard wrapper exposes configurable
beta, warm-up and update intervals. - Post-hoc synthesis requires multiple EMA settings and recorded checkpoints.
- Nested-module routing requires matching online and EMA submodule paths and compatible forward arguments.
- The documented examples show Python usage with PyTorch modules and caller-managed update steps.
Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.
Add an EMA model to a PyTorch training loop
Install the library, wrap a PyTorch module, update its EMA copy and invoke both models.
Before you start
- Python and PyTorch
- A PyTorch neural-network module
- A training loop that can call the EMA update method
- The ema-pytorch package
Steps
Install the package with the command below.
pip install ema-pytorchCreate the PyTorch network and wrap it with
EMA, setting values such asbeta,update_after_stepandupdate_every.After the network parameters are changed by the training process, call
ema.update().Invoke the live network or the EMA wrapper with input data; access the EMA copy through
ema.ema_modelwhen needed.Save the complete EMA wrapper when preserving its step count and warm-up state.
Expected outcome
A PyTorch model and an updating EMA counterpart are available through the documented wrapper interface.
Guide limitations
- The examples require the caller to integrate parameter updates and
ema.update()calls into its own training loop. - The documented recommendation is to save the complete wrapper when preserving step and warm-up information.
Configure post-hoc EMA synthesis
Record multiple EMA checkpoint streams during training and synthesize a model with another sigma-relative setting.
Before you start
- Python and PyTorch
- A PyTorch neural-network module
- At least two
sigma_relsvalues - A checkpoint folder for the recorded EMA checkpoints
- A loop that calls
emas.update()
Steps
Create
PostHocEMAaround the network with multiplesigma_rels, an update interval, checkpoint frequency and checkpoint folder.Run the training process and call
emas.update()as the network changes.After checkpoints have been collected, call
synthesize_ema_modelwith the desiredsigma_rel.Invoke the synthesized model with input data.
Expected outcome
A synthesized EMA model is produced from the recorded EMA checkpoints using the selected sigma-relative value.
Guide limitations
- The example states that at least two EMA settings are needed for synthesis.
- The method depends on checkpoints being recorded during the training process.
Sources & review
Source record details
- Source availability
- Active
- Sources checked
- 4 October 2026
- Original website title
- lucidrains/ema-pytorch · Official homepage title · retained in its source language

