skillfed

ema-pytorch

Easy way to keep track of exponential moving average version of your pytorch module

ema-pytorch v0.8.3 253.1K downloads/30d#8,526 on PyPI660
Permissive license MIT Active released

What it is and what it does

ema-pytorch wraps a PyTorch neural network to automatically maintain an exponential moving average (EMA) copy of its weights during training. Instead of using the latest model weights for inference or validation, you can use the EMA version, which often generalizes better by smoothing out noisy weight updates. The wrapper handles the decay schedule, update frequency, and optional warmup logic so you only need to call update() after each training step.

The package supports three main patterns: standard EMA with configurable decay and update frequency, post-hoc EMA synthesis (where multiple EMA versions are checkpointed and interpolated to create a new EMA at a different decay rate), and target representation routing for nested module architectures where EMA outputs are injected as teacher signals into student submodules. It is commonly used in diffusion models, self-supervised learning, and other training regimes where model averaging improves convergence or generalization.

Use it for:

  • Improve validation accuracy in diffusion models by using EMA weights instead of the latest training weights.
  • Implement teacher-student training in self-supervised learning where EMA outputs guide online module predictions.
  • Synthesize a new EMA model at a different decay rate from saved checkpoints without retraining.
  • Reduce overfitting in standard supervised learning by averaging weights across training steps.
  • Experiment with Switch EMA to update the online model with EMA weights periodically for better loss landscape flatness.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Maintains an exponential moving average (EMA) copy of a PyTorch model during training, with configurable decay, update frequency, and optional post-hoc synthesis across multiple EMA versions.

Yes. The package is lightweight, actively maintained, has no known vulnerabilities, and solves a well-defined problem (EMA tracking) that is standard in modern deep learning. Install it if you are training a PyTorch model and want to leverage weight averaging for better generalization or if you need post-hoc EMA synthesis for research.

Install

ema-pytorch on PyPI

pip

pip install ema-pytorch

uv

uv add ema-pytorch

poetry

poetry add ema-pytorch

Installing ema-pytorch

Before you install

Low friction: pure Python wheel with only torch as a runtime dependency. Active maintenance with a recent release (14 days old) and steady commit history.

License in practice

MIT license permits commercial and private use with minimal restrictions; you must include a copy of the license and copyright notice.

Quickstart

pip install ema-pytorch

import torch
from ema_pytorch import EMA

net = torch.nn.Linear(512, 512)
ema = EMA(net, beta=0.9999, update_after_step=100, update_every=10)

# After training updates to net:
ema.update()
output = ema(data)

Requires PyTorch to be installed; Python 3.8 or later.

Verify before relying

  • Whether post-hoc EMA synthesis (Karras et al.) offers measurable accuracy gains for your model architecture.
  • Performance overhead of EMA updates relative to model size and batch frequency.
  • Compatibility with distributed training frameworks (DDP, FSDP) not mentioned in the description.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 1 — torch
Maintenance actively maintained — 14 days since the last release
Last repo commit
First released
Downloads 253,140/month — #8,526 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: ema_pytorch-0.8.3-py3-none-any.whl

Keywords: artificial intelligence, deep learning, exponential moving average

Development Status :: 4 - BetaIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3.8Topic :: Scientific/Engineering :: Artificial Intelligence

Tags

exponential moving average pytorchEMA model wrapperpytorch model averagingteacher student EMAmodel weight smoothingtraining dynamics EMApost-hoc EMA synthesis
model-trainingweight-averagingdiffusion-models

More Artificial Intelligence packages