ebmkit
A small, reliable PyTorch toolkit for training and using energy-based models — the MCMC samplers, training losses, replay buffers, and diagnostics every EBM project otherwise rebuilds from scratch, as composable objects with tested defaults.
An EBM is an unnormalized density p(x) ∝ exp(-E(x)) defined by a network
E: (B, *shape) -> (B,). torch is the only runtime dependency.
Not the Explainable Boosting Machines that also go by "EBM" — this is the deep-learning kind (LeCun et al. 2006; Du & Mordatch 2019; Song & Kingma 2021).
Install
pip install ebmkit # runtime dependency is just torch>=2.0
pip install "ebmkit[viz]" # + matplotlib for the plotting helpers
The import name is ebm:
import torch, ebm
energy = ebm.nets.MLPEnergy(dim=2, hidden=(128, 128))
sampler = ebm.LangevinDynamics(step_size=1e-2, steps=60)
loss_fn = ebm.ContrastiveDivergence(sampler, buffer=ebm.ReplayBuffer(8192, (2,)))
trainer = ebm.Trainer(energy, loss_fn, lr=1e-3)
trainer.fit(ebm.datasets.two_moons(8192), steps=3000, batch_size=256)
samples = sampler.sample(energy, torch.randn(2000, 2), steps=500)
The Trainer is optional sugar — the loop underneath is plain PyTorch (each loss
returns LossOutput(loss, metrics, x_neg); call out.loss.backward()).
What's in the box
| Piece | Contents |
|---|---|
| Energies | any callable (B, *shape) -> (B,); nets.MLPEnergy / ConvEnergy / ConvClassifier (SiLU, optional spectral norm, no batch norm), nets.IsingEnergy / PottsEnergy (discrete lattices), noise-conditional variants for NCSN; EnergyModel, ebm.score |
| Samplers | LangevinDynamics (ULA/SGLD), MALA, HMC, GibbsWithGradients + CategoricalGibbsWithGradients, AnnealedLangevinDynamics |
| Losses | ContrastiveDivergence (CD-k / persistent CD), DiffusionRecoveryLikelihood + drl_sample, DenoisingScoreMatching / MultiSigmaDenoisingScoreMatching (NCSN), SlicedScoreMatching, NoiseContrastiveEstimation, JEMLoss |
| Composition | SumEnergy (product of experts), MixtureEnergy, TemperedEnergy — energies compose like densities and nest |
| Training | thin Trainer (device, EMA, supervised batches, save/load checkpointing), ReplayBuffer, EMA |
| Eval | ais_log_z / reverse_ais_log_z (bracket log Z), bits_per_dim, frechet_distance (FID), mmd, ood_auroc |
| Data & viz | 2D toys (two_moons, eight_gaussians, checkerboard, rings, spirals) and torchvision-free image loaders (mnist, fashion_mnist, cifar10, cifar100); viz.energy_contour / plot_samples / energy_histogram / show_images |
Docs
- Training methods — choosing a loss; CD/PCD, NCSN, DRL, NCE, JEM
- Samplers — Langevin/MALA/HMC, Gibbs-with-Gradients, annealed
- Evaluation — log-Z bracketing, FID, MMD, OOD, bits/dim
- Composition — products, mixtures, tempering
- Benchmarks — every loss family scored on the eval stack
Examples
Runnable scripts in examples/ (python examples/<name>.py):
train_two_moons.py— the canonical 2D contrastive-divergence smoke testtrain_jem.py/train_mnist_jem.py— classify, generate, and detect OOD with one networktrain_mnist.py— the image-scale IGEBM short-run recipetrain_composition.py— product of experts / mixture / tempering, without retrainingtrain_ising.py/train_potts.py— discrete lattices via (categorical) Gibbs-with-Gradientstrain_ncsn.py— score-based generation: multi-sigma denoising + annealed Langevintrain_cifar_ood.py— energy-based OOD at color scale (CIFAR-10 vs CIFAR-100)checkpoint_resume.py— save a run and resume it in a fresh process
Conventions
- Sign:
p ∝ exp(-E)— low energy is high probability. Samplers descend the energy gradient; training pushes data energy down. Never flip this. - Stop-gradients: MCMC negatives are detached and the energy's parameters are
frozen during sampling; score-matching losses instead keep the graph
(
create_graph=True). - The CD loss value is not a convergence signal — it hovers near zero at
equilibrium; watch
metrics["energy_gap"]and energy histograms.
See docs/ and CONTRIBUTING.md for the rest.
Development
uv run pytest # tests (CPU-only, seeded)
uv run ruff check . # lint
uv run mypy # type-check
Citation
If you use ebmkit in your research, please cite it — see CITATION.cff.
License
MIT — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ebmkit-0.12.0.tar.gz.
File metadata
- Download URL: ebmkit-0.12.0.tar.gz
- Upload date:
- Size: 3.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24f7a27c42e2f6bda84cb0eb53fc299eaa5da3ba8dbd31c51eadb0f4e981ed84
|
|
| MD5 |
552a895fe089589727cd24e3ccaed8a6
|
|
| BLAKE2b-256 |
babc4dda529d7f47f0b85d3b7078e1a7516a4433592b7cdf0b3031d7892af7de
|
Provenance
The following attestation bundles were made for ebmkit-0.12.0.tar.gz:
Publisher:
publish.yml on davidkhjo/ebmkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ebmkit-0.12.0.tar.gz -
Subject digest:
24f7a27c42e2f6bda84cb0eb53fc299eaa5da3ba8dbd31c51eadb0f4e981ed84 - Sigstore transparency entry: 2466786783
- Sigstore integration time:
-
Permalink:
davidkhjo/ebmkit@3e5e293fb08a714b72ec65d2d89722f4fcf1b6b7 -
Branch / Tag:
refs/tags/v0.12.0 - Owner: https://github.com/davidkhjo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3e5e293fb08a714b72ec65d2d89722f4fcf1b6b7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ebmkit-0.12.0-py3-none-any.whl.
File metadata
- Download URL: ebmkit-0.12.0-py3-none-any.whl
- Upload date:
- Size: 46.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a642daefe9e55a2c3160d857aae0cb8c00936242a4f37872f7f1136731a6c4f
|
|
| MD5 |
500bff02dad13778a43093bddbeb4c8d
|
|
| BLAKE2b-256 |
f3564eac83479d3d59424d2061927438a522dcfba21b50abd2c56dd94735601b
|
Provenance
The following attestation bundles were made for ebmkit-0.12.0-py3-none-any.whl:
Publisher:
publish.yml on davidkhjo/ebmkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ebmkit-0.12.0-py3-none-any.whl -
Subject digest:
7a642daefe9e55a2c3160d857aae0cb8c00936242a4f37872f7f1136731a6c4f - Sigstore transparency entry: 2466786826
- Sigstore integration time:
-
Permalink:
davidkhjo/ebmkit@3e5e293fb08a714b72ec65d2d89722f4fcf1b6b7 -
Branch / Tag:
refs/tags/v0.12.0 - Owner: https://github.com/davidkhjo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3e5e293fb08a714b72ec65d2d89722f4fcf1b6b7 -
Trigger Event:
release
-
Statement type: