Learning Objectives¶
The objectives in torchebm.losses fall into two groups. Energy objectives fit a potential \(E_\theta\) with \(p_\theta(x) \propto e^{-E_\theta(x)}\); maximum likelihood gives the gradient
a push-down on data and a push-up on model samples, and the energy objectives differ in how they treat the expectation under the model: contrastive divergence estimates it with MCMC, score matching avoids it by fitting \(-\nabla_x E_\theta\). Transport objectives fit a model to a probability path by regression and need no model samples; the model is a velocity, a time-invariant field, or a potential trained through its gradient (EqM-E, energy matching). 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
BaseLoss --> ContrastiveDivergenceLoss
BaseScoreMatching --> DenoisingScoreMatching
BaseLoss --> BaseScoreMatching
BaseLoss --> DenoisingScoreMatchingLoss
BaseInterpolantLoss --> EnergyMatchingLoss
BaseLoss --> BaseInterpolantLoss
BaseInterpolantLoss --> EquilibriumMatchingLoss
BaseInterpolantLoss --> FlowMatchingLoss
BaseLoss --> MeanFlowLoss
BaseContrastiveDivergence --> PersistentContrastiveDivergence
BaseScoreMatching --> ScoreMatching
BaseLoss --> ScoreMatchingLoss
BaseLoss --> ShortcutLoss
BaseScoreMatching --> SlicedScoreMatching
BaseLoss --> SlicedScoreMatchingLoss MCMC-based: contrastive divergence¶
CD-k approximates model samples with k steps of MCMC started at the data1:
Starting the chains from a ReplayBuffer instead (sampler(model, buffer.sample(n), n_steps=10), then buffer.add(negatives)) gives PCD: negatives resume across updates, so chains explore the model distribution beyond k steps. CD pays for an inner MCMC loop at every update and in exchange fits the energy by approximate maximum likelihood.
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 (the model is built with prediction_type="score", so its call returns \(-\nabla_x E\)),
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¶
Transport objectives are simulation-free: the regression target comes from an interpolant path, so no sampler runs inside the loss (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. The path objects do the data work in the loop: an interpolant builds x_t and the target u_t, a coupling pairs noise with data, compute_density_for_timestep_sampling skews t, and a per-sample weight= reweights the loss.
Equilibrium matching trains a time-invariant model toward c(t) u_t, the path velocity from noise to data scaled by c(t), the truncated decay of the paper, which vanishes at the data. FlowSampler integrates the learned field over a fixed horizon, and the gradient-descent sampler iterates \(x \leftarrow x + \eta f(x)\) toward its attractors; both apply the same update with different step sizes and horizons. With the implicit formulation the model is a time-invariant velocity field (a model that declares prediction_type="velocity", such as DiT, needs the same on the gradient samplers), and it is not constrained to be the gradient of an energy; its population optimum is not one in general, because the target averages noise levels with weights that depend on \(x\). With the explicit one (EqM-E) the model is a scalar potential called with prediction_type="score", so its field is a gradient by construction; InteractionModel wraps its energy for diverse sampling, and the energy scores OOD points directly.
The two losses differ only in the scaling of the regression target and in the clock the model sees:
| Loss | Target | Clock shown to the model | Sample with |
|---|---|---|---|
FlowMatchingLoss | u_t (noise -> data) | t | FlowSampler |
EquilibriumMatchingLoss | c(t) u_t | none (time-invariant) | FlowSampler, fixed-point iteration with GradientDescentSampler |
With ct="constant" and ct_multiplier=1 the EqM objective is bit-identical to FlowMatchingLoss.
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. The potential returns its energy and score from one call; the warm-up calls loss_fn(score, u_t, t) alone, the joint phase adds the energies of data and Langevin negatives. The coupling pairs the batch in the loop and weight= takes its per-pair weights:
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 (implicit) or energy (EqM-E) | FlowSampler ODE, or iteration of the field | generative quality with few integration steps |
| Energy matching | phase 2 only | energy | one Langevin sweep | one potential for both transport and Boltzmann sampling |
Objectives with inner sampling fit an energy by approximate maximum likelihood at the cost of MCMC at every update. Transport objectives train by regression and generate in a fixed number of steps; equilibrium and energy matching apply that regime to time-invariant models, and EqM-E and energy matching keep a scalar energy.
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. ↩