mixle.models.energy module¶
EnergyModel – an energy-based density p(x) ∝ exp(-E(x)) as a composable mixle leaf.
The one neural density whose normalizer is intractable: p(x) = exp(-E(x)) / Z with Z = ∫ exp(-E(x)) dx
unavailable in closed form. So unlike the flows (exact) it is trained and scored approximately, and this is
stated plainly – it is the energy-model analogue of the VAE’s ELBO caveat.
Training is Noise-Contrastive Estimation (Gutmann & Hyvärinen 2010), not maximum likelihood: the model learns to tell data from samples of a known noise distribution, and in doing so learns a scalar log-normalizer
calongside the energy net. NCE is consistent – as data grow,c -> log Zand-E(x) + c -> log p(x)– solog_density(x) = -E(x) + cis an approximately normalized log-density, usable directly (no per-evaluation partition estimate). It composes in a mixture, but being only approximately normalized it can bias mixture weights against an exact leaf (same honesty caveat as the VAE).Sampling is unnormalized-density MCMC: a few steps of Langevin dynamics
x <- x - s ∇E(x) + sqrt(2s) ε.
Its value over the flows is the inductive bias: an energy net imposes no ordering and no invertibility – it scores
compatibility, so it captures undirected/symmetric structure a coupling or autoregressive flow parameterizes
awkwardly. build_energy_net() is a ready MLP energy to wrap.
- class EnergyModel(module, *, m_steps=200, lr=5e-3, noise_ratio=1, langevin_steps=40, langevin_step=0.05, device='cpu', name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionlog p(x) ≈ -E(x) + cfor an energy module (module.energy(x) -> (n,)and a learned scalarlog_norm).Approximately normalized (trained by NCE);
log_densityreturns-E(x) + c. Composes like any leaf.- Parameters:
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
EnergyModelSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
EnergyModelEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
EnergyModelEncoder
- to_dict()[source]
Return a safe JSON-compatible representation of this distribution.
- class EnergyModelSampler(dist, seed=None)[source]
Bases:
DistributionSamplerLangevin dynamics on the (unnormalized) energy:
x <- x - s ∇E(x) + sqrt(2 s) ε.- Parameters:
dist (EnergyModel)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class EnergyModelEncoder[source]
Bases:
DataSequenceEncoder
- class EnergyModelAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulatorBuffers responsibility-weighted data for the NCE M-step (weights = the E-step soft counts).
- update(x, weight, estimate)[source]
- seq_update(enc, weights, estimate)[source]
- seq_initialize(enc, weights, rng)[source]
- acc_to_encoder()[source]
- Return type:
EnergyModelEncoder
- class EnergyModelAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- Return type:
EnergyModelAccumulator
- class EnergyModelEstimator(module, *, m_steps=200, lr=5e-3, noise_ratio=1, langevin_steps=40, langevin_step=0.05, device='cpu', name=None)[source]
Bases:
ParameterEstimatorM-step: Noise-Contrastive Estimation against a Gaussian noise fit to the (weighted) data.
Learns the energy net and the scalar log-normalizer
log_normby logistic discrimination of data from noise – so the resulting-E(x) + log_normis a consistent, approximately-normalized log-density.- Parameters:
- accumulator_factory()[source]
- Return type:
EnergyModelAccumulatorFactory
- build_energy_net(dim, *, hidden=64, layers=3)[source]
An MLP energy
E(x): R^dim -> R(plus a learned scalarlog_norm) – ready to wrap in an EnergyModel.Lower energy = higher (unnormalized) density.
log_normis the NCE-learned normalizer, so the pairedEnergyModelscores-E(x) + log_norm. Swap in any module exposingenergy(x) -> (n,), alog_normparameter and adimattribute.