Energy-Based Models: How Energy Landscapes, Contrastive Divergence, and Langevin Dynamics Unify Generative Learning

Energy-Based Models: How Energy Landscapes, Contrastive Divergence, and Langevin Dynamics Unify Generative Learning Probabilistic modeling in machine learning fundamentally centers on estimating data distributions over high-dimensional spaces. Standard generative architectures achieve this by enforcing structural constraints: autoregressive models factorize joint distributions through causal chains, normalizing flows constrain network architectures to invertible bijections with tractable Jacobi

9 min
Energy-Based Models: How Energy Landscapes, Contrastive Divergence, and Langevin Dynamics Unify Generative Learning

Energy-Based Models: How Energy Landscapes, Contrastive Divergence, and Langevin Dynamics Unify Generative Learning

Probabilistic modeling in machine learning fundamentally centers on estimating data distributions over high-dimensional spaces. Standard generative architectures achieve this by enforcing structural constraints: autoregressive models factorize joint distributions through causal chains, normalizing flows constrain network architectures to invertible bijections with tractable Jacobian determinants, variational autoencoders optimize surrogate lower bounds over latent spaces, and generative adversarial networks bypass density estimation through two-player minimax games.

Energy-Based Models (EBMs) eliminate these architectural constraints. An EBM maps any arbitrary input configuration to a single unconstrained real scalar value termed its energy. By assigning low energy values to plausible data points and high energy values to implausible or corrupt configurations, EBMs define flexible probability densities that underpin modern score-based diffusion, modern Hopfield networks, self-supervised representation learning, and preference alignment algorithms.

Contrastive Divergence and Langevin Dynamics in Energy-Based Models

The Boltzmann-Gibbs Formulation and the Intractable Partition Function

Formally, an energy-based model defines the probability density pθ(x)p_\theta(x) over a continuous domain XRD\mathcal{X} \subseteq \mathbb{R}^D using a parameterized neural network Eθ(x):RDRE_\theta(x): \mathbb{R}^D \to \mathbb{R}:

pθ(x)=exp(Eθ(x))Z(θ)p_\theta(x) = \frac{\exp(-E_\theta(x))}{Z(\theta)}

The normalizing factor Z(θ)Z(\theta), known as the partition function in statistical physics, integrates the unnormalized Boltzmann factor over the entire input space:

Z(θ)=Xexp(Eθ(x))dxZ(\theta) = \int_{\mathcal{X}} \exp(-E_\theta(x)) \, dx

Because the partition function requires integration over high-dimensional continuous domains, exact computation of Z(θ)Z(\theta) is analytically and computationally intractable for non-trivial neural energy functions.

When training an EBM via maximum likelihood estimation on an empirical dataset D={x1,,xN}\mathcal{D} = \{x_1, \dots, x_N\}, the objective minimizes the negative log-likelihood loss:

L(θ)=1Ni=1Nlogpθ(xi)=1Ni=1NEθ(xi)+logZ(θ)\mathcal{L}(\theta) = -\frac{1}{N} \sum_{i=1}^N \log p_\theta(x_i) = \frac{1}{N} \sum_{i=1}^N E_\theta(x_i) + \log Z(\theta)

Taking the gradient of the log-likelihood with respect to model parameters θ\theta yields:

θlogpθ(x)=θEθ(x)θlogZ(θ)\nabla_\theta \log p_\theta(x) = -\nabla_\theta E_\theta(x) - \nabla_\theta \log Z(\theta)

Evaluating the gradient of the log-partition function reveals a fundamental identity:

θlogZ(θ)=1Z(θ)θXexp(Eθ(x))dx=Xexp(Eθ(x))Z(θ)(θEθ(x))dx=Expθ(x)[θEθ(x)]\nabla_\theta \log Z(\theta) = \frac{1}{Z(\theta)} \nabla_\theta \int_{\mathcal{X}} \exp(-E_\theta(x)) \, dx = \int_{\mathcal{X}} \frac{\exp(-E_\theta(x))}{Z(\theta)} \left(-\nabla_\theta E_\theta(x)\right) dx = -\mathbb{E}_{x^- \sim p_\theta(x)}[\nabla_\theta E_\theta(x^-)]

Substituting this identity back into the log-likelihood gradient reveals the dual-phase learning dynamic that governs all energy-based training:

θlogpθ(x)=θEθ(xdata)Positive Phase (Pulls Down Data Energy)+Expθ(x)[θEθ(x)]Negative Phase (Pushes Up Hallucination Energy)\nabla_\theta \log p_\theta(x) = \underbrace{-\nabla_\theta E_\theta(x_{\text{data}})}_{\text{Positive Phase (Pulls Down Data Energy)}} + \underbrace{\mathbb{E}_{x^- \sim p_\theta(x)}[\nabla_\theta E_\theta(x^-)]}_{\text{Negative Phase (Pushes Up Hallucination Energy)}}

In the positive phase, the optimizer adjusts parameters to decrease the energy assigned to observed training samples xdatax_{\text{data}}. In the negative phase, the optimizer increases the energy of negative samples xx^- drawn from the model's current predictive distribution pθ(x)p_\theta(x). Without the negative phase, the energy function suffers collapse, assigning uniform zero energy across all inputs.

Sampling from Energy Landscapes: Langevin Dynamics

Because pθ(x)p_\theta(x) is unnormalized and lacks ancestral factorization, standard forward sampling cannot generate synthetic samples in a single pass. Instead, sampling relies on Markov Chain Monte Carlo (MCMC) algorithms, primarily Stochastic Gradient Langevin Dynamics (SGLD) introduced by Welling and Teh (2011).

Langevin dynamics simulates Brownian motion across the continuous potential landscape defined by Eθ(x)E_\theta(x). Given an initial sample x0x_0 drawn from a broad prior distribution (such as uniform noise U[1,1]D\mathcal{U}[-1, 1]^D), the chain updates iteratively over discrete time steps tt:

xt+1=xtϵ2xEθ(xt)+ϵzt,ztN(0,I)x_{t+1} = x_t - \frac{\epsilon}{2} \nabla_x E_\theta(x_t) + \sqrt{\epsilon} z_t, \quad z_t \sim \mathcal{N}(0, I)

Here, ϵ>0\epsilon > 0 represents the step size, xEθ(xt)\nabla_x E_\theta(x_t) is the spatial gradient of the energy function evaluated with respect to the input, and ztz_t is standard isotropic Gaussian noise.

The update consists of two opposing forces:

  1. Deterministic Drift (ϵ2xEθ(xt)-\frac{\epsilon}{2} \nabla_x E_\theta(x_t)): Guides the state vector down the steepest energy descent path toward high-probability attractor basins.
  2. Stochastic Diffusion (ϵzt\sqrt{\epsilon} z_t): Injects Brownian fluctuations to prevent the trajectory from getting trapped in shallow local minima and ensures exploration of the full probability density.

As the step size ϵ0\epsilon \to 0 and the number of steps tt \to \infty, the distribution of samples generated by this Langevin diffusion process converges asymptotically to the exact model distribution pθ(x)p_\theta(x).

To scale Langevin sampling to high-dimensional continuous domains like images, Du and Mordatch (2019) introduced key stability mechanisms:

  • Persistent Replay Buffers: Instead of initializing Langevin chains from pure noise at every training iteration, negative samples are initialized from a historical replay buffer with high probability (e.g., 95%) and from random noise with low probability (5%). This allows MCMC chains to maintain persistent trajectories across training iterations.
  • Gradient Clipping: Clamping xEθ(x)\nabla_x E_\theta(x) prevents exploding gradient steps caused by steep energy cliffs.
  • Langevin Step Schedules: Running 40 to 100 Langevin steps with decaying step sizes per training step provides stable convergence on complex benchmarks.

Training EBMs: Contrastive Divergence and Score Matching

Running MCMC chains to full asymptotic equilibrium at every training step is computationally prohibitive. Several mathematical frameworks bypass full equilibrium sampling:

Contrastive Divergence (CDkCD_k)

Introduced by Hinton (2002), Contrastive Divergence replaces full stationary MCMC sampling with short, non-converged Markov chains. Rather than initializing from random noise, CDkCD_k initializes the Markov chain directly at the empirical data point x(0)=xdatax^{(0)} = x_{\text{data}} and executes only kk steps of Gibbs or Langevin transitions:

x(0)pdatak stepsx(k)x^{(0)} \sim p_{\text{data}} \xrightarrow{k \text{ steps}} x^{(k)}

The parameter update approximates the true log-likelihood gradient using this truncated sample:

ΔθθEθ(x(0))+θEθ(x(k))\Delta \theta \approx -\nabla_\theta E_\theta(x^{(0)}) + \nabla_\theta E_\theta(x^{(k)})

Even with k=1k=1, CD1CD_1 provides an effective, though slightly biased, optimization direction that pulls down data energy while lifting energy in the immediate local neighborhood of observed data.

Score Matching and Denoising Score Matching

Instead of estimating the unnormalized density pθ(x)p_\theta(x), Hyvärinen (2005) proposed matching the score function, defined as the spatial gradient of the log-density with respect to input xx:

sθ(x)=xlogpθ(x)=xEθ(x)xlogZ(θ)=0=xEθ(x)s_\theta(x) = \nabla_x \log p_\theta(x) = -\nabla_x E_\theta(x) - \underbrace{\nabla_x \log Z(\theta)}_{= 0} = -\nabla_x E_\theta(x)

Because the partition function Z(θ)Z(\theta) depends only on parameters θ\theta and does not vary with input xx, its spatial derivative xlogZ(θ)\nabla_x \log Z(\theta) is identically zero. The score function is independent of the partition function.

Explicit score matching minimizes the expected squared error between the model score and the true data score:

JESM(θ)=12Expdata[xlogpθ(x)xlogpdata(x)2]\mathcal{J}_{\text{ESM}}(\theta) = \frac{1}{2} \mathbb{E}_{x \sim p_{\text{data}}}\left[\|\nabla_x \log p_\theta(x) - \nabla_x \log p_{\text{data}}(x)\|^2\right]

Using integration by parts, Hyvärinen eliminated the unobservable ground-truth score xlogpdata(x)\nabla_x \log p_{\text{data}}(x), rewriting the objective as:

JSM(θ)=Expdata[Tr(x2logpθ(x))+12xlogpθ(x)2]\mathcal{J}_{\text{SM}}(\theta) = \mathbb{E}_{x \sim p_{\text{data}}}\left[\text{Tr}(\nabla_x^2 \log p_\theta(x)) + \frac{1}{2} \|\nabla_x \log p_\theta(x)\|^2\right]

To eliminate the expensive computation of the Hessian trace Tr(x2logpθ(x))\text{Tr}(\nabla_x^2 \log p_\theta(x)), Vincent (2011) developed Denoising Score Matching (DSM). By corrupting inputs with known Gaussian noise qσ(x~x)=N(x~;x,σ2I)q_\sigma(\tilde{x}|x) = \mathcal{N}(\tilde{x}; x, \sigma^2 I), the analytical score of the perturbation kernel x~logqσ(x~x)=x~xσ2\nabla_{\tilde{x}} \log q_\sigma(\tilde{x}|x) = -\frac{\tilde{x} - x}{\sigma^2} is known, yielding the simplified objective:

LDSM(θ)=Expdata,x~qσ(x~x)[sθ(x~)+x~xσ22]\mathcal{L}_{\text{DSM}}(\theta) = \mathbb{E}_{x \sim p_{\text{data}}, \tilde{x} \sim q_\sigma(\tilde{x}|x)}\left[\left\|s_\theta(\tilde{x}) + \frac{\tilde{x} - x}{\sigma^2}\right\|^2\right]

The Unifying Bridge to Modern Generative AI and LLMs

Energy-based formulations serve as the underlying mathematical scaffolding across several distinct domains in modern artificial intelligence:

1. Score-Based Diffusion Models

In a landmark synthesis, Song and Ermon (2019) connected score matching on energy landscapes to generative diffusion models. A score-based diffusion model trains a noise-conditional neural network sθ(x,σ)s_\theta(x, \sigma) to predict energy gradients across a geometric ladder of perturbation scales σ1<σ2<<σL\sigma_1 < \sigma_2 < \dots < \sigma_L.

Sampling then executes Annealed Langevin Dynamics, initiating MCMC at the highest noise scale σL\sigma_L where the energy landscape is smooth and unimodal, and gradually decreasing the noise level toward σ1\sigma_1. This resolves the classic mode-mixing failure of standard EBMs by smoothing out steep energy barriers at initial generation stages.

2. Joint Energy-Based Models (JEM)

Grathwohl et al. (2019) proved that any standard discriminative neural network classifier is secretly an energy-based model. For a classifier parameterized by fθ(x):RDRKf_\theta(x): \mathbb{R}^D \to \mathbb{R}^K producing logits for KK discrete classes, the joint probability over inputs and classes is defined as:

pθ(x,y)=exp(fθ(x)[y])Z(θ)p_\theta(x, y) = \frac{\exp(f_\theta(x)[y])}{Z(\theta)}

Marginalizing over all class labels yy reveals the implicit marginal distribution of input xx:

pθ(x)=y=1Kpθ(x,y)=y=1Kexp(fθ(x)[y])Z(θ)=exp(LogSumExpy(fθ(x)[y]))Z(θ)p_\theta(x) = \sum_{y=1}^K p_\theta(x, y) = \frac{\sum_{y=1}^K \exp(f_\theta(x)[y])}{Z(\theta)} = \frac{\exp\left(\text{LogSumExp}_y(f_\theta(x)[y])\right)}{Z(\theta)}

This defines an unnormalized energy function directly from classifier logits:

Eθ(x)=LogSumExpy(fθ(x)[y])E_\theta(x) = -\text{LogSumExp}_y(f_\theta(x)[y])

By training this objective with a hybrid loss combining standard cross-entropy classification and Langevin-based contrastive divergence, a single model performs competitive classification, state-of-the-art out-of-distribution detection, and unconditional generative synthesis simultaneously.

3. Modern Continuous Hopfield Networks

Ramsauer et al. (2020) demonstrated that the self-attention mechanism in Transformer architectures is mathematically equivalent to the state update rule of a modern continuous Hopfield network. The energy function governing stored memory patterns {x1,,xN}\{x_1, \dots, x_N\} and state vector ξ\xi is given by:

E(ξ)=lse(β,XTξ)+12ξ2+CE(\xi) = -\text{lse}\left(\beta, X^T \xi\right) + \frac{1}{2} \|\xi\|^2 + C

Applying concave-convex procedure minimization to this energy function yields the exact scaled dot-product softmax attention formula:

ξnew=Xsoftmax(βXTξ)\xi^{\text{new}} = X \, \text{softmax}\left(\beta X^T \xi\right)

Transformers can thus be understood as dynamical systems performing energy minimization over associative memory representations.

4. Preference Alignment in Large Language Models

The energy-based formulation also underpins modern reinforcement learning from human feedback. In Direct Preference Optimization (DPO), the Bradley-Terry preference probability for a preferred completion ywy_w over a dispreferred completion yly_l given prompt xx is defined as:

p(ywylx)=σ(r(x,yw)r(x,yl))p(y_w \succ y_l | x) = \sigma\left(r(x, y_w) - r(x, y_l)\right)

Under optimal policy derivations, the scalar reward r(x,y)r(x, y) corresponds directly to the negative energy of the completion under the fine-tuned model relative to a frozen reference model:

r(x,y)=βlogπθ(yx)πref(yx)Eθ(y;x)r(x, y) = \beta \log \frac{\pi_\theta(y|x)}{\pi_{\text{ref}}(y|x)} \equiv -E_\theta(y; x)

DPO optimizes an energy landscape where preferred tokens have their energy decreased (likelihood increased) and dispreferred tokens have their energy increased (likelihood decreased), exactly mirroring the positive and negative phases of contrastive divergence.

Architectural Trade-Offs and System Characteristics

| Dimension | Energy-Based Models (EBM) | Diffusion / Score Models | Autoregressive Transformers | Flow Matching | | :--- | :--- | :--- | :--- | :--- | | Density Evaluation | Unnormalized (Eθ(x)E_\theta(x) only; ZZ intractable) | Approximate lower bound via SDE integration | Exact normalized likelihood via causal chain rule | Exact normalized likelihood via Continuous Normalizing Flows | | Sampling Mechanism | Iterative MCMC / Langevin dynamics | Iterative reverse SDE/ODE solvers | Step-by-step causal token generation | Numerical ODE integration (Euler / RK4) | | Architectural Restrictions | Completely unconstrained (f(x)Rf(x) \to \mathbb{R}) | Must output vector equal to input dimension | Causal autoregressive masking required | Vector field regressor (vt(x)RDv_t(x) \in \mathbb{R}^D) | | Training Stability | Moderate to challenging (MCMC divergence risk) | High (stable score matching loss) | High (convex cross-entropy per step) | High (direct regression objective) | | Inference Latency | High (tens to hundreds of gradient steps) | High to moderate (10-50 ODE steps) | Proportional to sequence length (O(N)O(N) forward passes) | Low to moderate (10-30 ODE integration steps) |

Core Failure Modes in High-Dimensional Energy Landscapes

  1. Metastable States and Slow MCMC Mixing: In high-dimensional spaces with complex data distributions, probability mass clusters in isolated modes separated by massive energy barriers. Standard Langevin dynamics requires exponential time to cross these barriers, causing negative sampling chains to over-sample local modes while missing others.
  2. Out-of-Distribution Energy Overestimation: Without explicit regularization or negative sampling across the entire input domain, neural energy functions frequently assign uncharacteristically low energy to bizarre, non-natural inputs lying far outside the training distribution manifold.
  3. Training Instability: If the Langevin step size ϵ\epsilon or gradient norm grows too large during negative phase sampling, MCMC chains can diverge into numerical overflow, destabilizing the entire parameter optimization process.

Summary

Energy-Based Models provide a mathematically unified perspective on probabilistic learning. By discarding the requirement that neural outputs integrate to unity, EBMs decouple representation architectures from density normalization. While training unnormalized models requires navigating the intractable partition function via contrastive divergence, score matching, or Langevin dynamics, the resulting energy principles directly inform score-based diffusion, attention dynamics, associative memories, and alignment objectives across modern artificial intelligence.

Sources

Written by

More to read

  • XPeng Robotics Raises Over 00M at .3B Valuation to Scale Humanoid Robot Production

    Chinese electric vehicle manufacturer XPeng has announced that its robotics affiliate raised over $900 million in its first major institutional financing round. The investment values the robotics business at more than $6.3 billion post-money, representing one of the largest single private capital raises in the embodied AI sector to date. The round was led by IDG Capital and Gaorong Ventures, with participation from strategic tech conglomerates Tencent and Alibaba alongside parent firm XPeng Inc

    1 min
  • Alibaba Launches Wan 3.0 AI Video Model with Native 30-Second Generation and Document Inputs

    Alibaba Tongyi Lab has launched a public beta of Wan 3.0, the latest iteration of its video generation model family. Available on Alibaba Cloud Model Studio and Qwen Cloud under the model identifier wan3.0-video, the model produces up to 30 seconds of continuous video in a single pass at resolutions up to 1080p. Unlike predecessor models such as Wan 2.7, which capped single-pass output at 15 seconds, Wan 3.0 consolidates video synthesis into a unified architecture and expands supported input mo

    1 min
  • Long-Context Evaluation in Production: Why Synthetic Needles Fail, Multi-Hop Stress Testing, and Benchmarking Effective Context Size

    Frontier model providers frequently market sequence lengths spanning 128K, 1M, or 2M tokens. However, engineering teams deploying these models in production environments for codebase refactoring, multi-turn agent execution, or complex document analysis regularly observe severe retrieval failures and reasoning breakdowns well before hitting context boundaries. The primary culprit behind this disconnect is the widespread reliance on vanilla Needle In A Haystack (NIAH) evaluations. While standard

    1 min