Learning Objectives¶
Fitting \(p_\theta(x) \propto e^{-E_\theta(x)}\) by maximum likelihood gives the gradient
a push-down on data and a push-up on model samples. Every objective in torchebm.losses is a different answer to the hard part, the expectation under the model itself. The shipped family, generated from the installed package at build time:
graph TD
BaseContrastiveDivergence(["BaseContrastiveDivergence"])
BaseInterpolantLoss(["BaseInterpolantLoss"])
BaseLoss(["BaseLoss"])
BaseScoreMatching(["BaseScoreMatching"])
BaseContrastiveDivergence --> ContrastiveDivergence
BaseLoss --> BaseContrastiveDivergence
BaseScoreMatching --> DenoisingScoreMatching
BaseLoss --> BaseScoreMatching
BaseInterpolantLoss --> EnergyMatchingLoss
BaseLoss --> BaseInterpolantLoss
BaseInterpolantLoss --> EquilibriumMatchingLoss
BaseInterpolantLoss --> FlowMatchingLoss
BaseContrastiveDivergence --> PersistentContrastiveDivergence
BaseScoreMatching --> ScoreMatching
BaseScoreMatching --> SlicedScoreMatching MCMC-based: contrastive divergence¶
CD-k approximates model samples with k steps of MCMC started at the data1:
persistent=True switches to PCD: negatives resume from a replay buffer instead of restarting at the data, so chains explore the model distribution across updates. CD trains slowly per step (an inner MCMC loop) but yields a genuine energy with meaningful level sets.
Simulation-free: score matching¶
Score matching sidesteps model samples entirely by fitting the score2. The exact objective needs the Hessian trace; the practical members are denoising SM3, which matches the score of noise-perturbed data,
and sliced SM, which estimates the trace with random projections. DSM at a ladder of noise scales is the training principle underlying score-based diffusion4.
Transport-based: flow, equilibrium and energy matching¶
The modern objectives are simulation-free: they replace the inner sampling loop with regression along an interpolant path (see Interpolants and Couplings).
Flow matching regresses a time-conditioned velocity field v(x, t) onto the interpolant velocity u_t and generates by integrating it forward with FlowSampler (no negation). It shares the transport surface of the other matching losses: interpolant=, coupling= with per-pair weights, t_sampler= (uniform or the EDM lognormal skew), and a per-timestep loss_weight_fn.
Equilibrium matching trains a time-invariant field f(x) toward the noise direction along the path (f points data -> noise), so every route transports noise -> data by moving along -f. With the implicit formulation (energy_type="none") f is the gradient field: integrate it with FlowSampler(negate_velocity=True), or descend it with the EqMEnergy adapter, which turns the field into the scalar BaseModel the gradient-based samplers and InteractionModel consume. The explicit formulation (energy_type="dot") trains the scalar energy g(x) = x . f(x) for gradient-descent sampling and OOD scoring. It also accepts a coupling= (default identity) and, like energy matching, honors per-pair coupling weights.
EqMEnergy.from_loss picks the adapter mode matching the trained energy_type (implicit vs dot/l2), so the sampled energy always matches what was trained. InteractionModel must wrap an explicit energy, never the implicit adapter.
The two losses differ only in the sign of the regression target and in the clock the model sees; each has a switch to adopt the other's convention:
| Loss | Target | Clock shown to the model | Sample with |
|---|---|---|---|
FlowMatchingLoss | u_t (noise -> data) | sampled t | FlowSampler |
FlowMatchingLoss(negate_velocity=True) | -u_t (data -> noise) | sampled t | FlowSampler(negate_velocity=True), descent samplers via EqMEnergy |
EquilibriumMatchingLoss | -u_t * c(t) | zeros (time_invariant=True) | FlowSampler(negate_velocity=True), EqMEnergy |
EquilibriumMatchingLoss(time_invariant=False) | -u_t * c(t) | sampled t | FlowSampler(negate_velocity=True) |
With ct="constant", ct_multiplier=1 and time_invariant=False the EqM objective is bit-identical to FlowMatchingLoss(negate_velocity=True). EqMEnergy always evaluates the field at t = 0, so it suits time-invariant fields.
Energy matching (arXiv:2504.10612) keeps a single time-independent scalar potential: an OT flow-matching warm-up shapes it as transport, then a contrastive phase with temperature-scheduled Langevin negatives sharpens its Boltzmann density near the data. It accepts a coupling= and consumes per-pair weights when the coupling provides them:
Choosing an objective¶
| Objective | Inner sampling | Trains | Generate with | Reach for it when |
|---|---|---|---|---|
| CD / PCD / PT-CD | yes (k MCMC steps) | energy | MCMC | you need a calibrated energy and can afford MCMC per step |
| Exact / sliced SM | no (Hessian term) | energy | Langevin | low dimension, no noise tolerance |
| Denoising SM | no | energy (smoothed) | annealed Langevin | fast sampling-free training, noise scale acceptable |
| Flow matching | no | velocity field | FlowSampler ODE/SDE | pure generative transport, standard diffusion-style recipes |
| Equilibrium matching | no | field or energy | FlowSampler ODE or EqMEnergy + gradient descent | generative quality with few integration steps |
| Energy matching | phase 2 only | energy | one Langevin sweep | one potential for both transport and Boltzmann sampling |
The rule of thumb embedded in the table: objectives with inner sampling buy energy fidelity at training cost; transport objectives buy training scalability and fast generation, and the hybrids exist to keep the energy while paying the transport price.
Runnable counterparts¶
- CD-k on Two Moons
- Persistent CD
- Denoising Score Matching
- Equilibrium Matching in 2D
- Energy Matching in 2D
-
G. E. Hinton. Training products of experts by minimizing contrastive divergence. Neural Computation, 14(8), 2002. ↩
-
A. Hyvärinen. Estimation of non-normalized statistical models by score matching. JMLR, 6, 2005. ↩
-
P. Vincent. A connection between score matching and denoising autoencoders. Neural Computation, 23(7), 2011. ↩
-
Y. Song and S. Ermon. Generative modeling by estimating gradients of the data distribution. NeurIPS, 2019. ↩