DL0186 Energy-Based Models and Contrastive Divergence

What is the Energy-Based Model (EBM) formulation, and how does Contrastive Divergence approximate the intractable partition function gradient without sampling full Markov chains?

Answer

An energy-based model refuses to parameterize a normalized density directly. It learns only a scalar energy function E_{\theta}(x), low for plausible configurations and high for implausible ones, and recovers a density by exponentiating and normalizing. The price of that freedom is the partition function Z(\theta), an integral over the whole input space that is intractable for anything beyond a toy problem. Differentiating the log-likelihood splits into a positive phase that lowers energy at observed data and a negative phase that raises energy wherever the model currently places mass, and the negative phase is an expectation under p_{\theta} that normally needs MCMC samples at equilibrium. Contrastive divergence keeps the same two-phase update but initializes the chain at the training data rather than at noise, then runs only k transition steps, in practice k = 1. The estimate is biased because the k-step distribution is not the equilibrium one, yet the bias shrinks near the optimum where the data distribution is already close to a fixed point of the sampler, and the variance is far lower than that of a long chain.

(1) Energy, Not Probability: the network outputs one unconstrained scalar per input, so any architecture works and no normalization layer or invertibility constraint is required.
(2) The Partition Function Is The Whole Problem: Z(\theta) is a sum or integral over every possible configuration, so exact likelihood and exact sampling are both out of reach.
(3) Positive And Negative Phase: the exact gradient is a difference of two expectations of \nabla_{\theta}E, one under the data and one under the model, which is why training is often described as pushing energy down at real points and up at fantasy points.
(4) Chain Started At The Data: CD replaces the equilibrium expectation with samples from a chain seeded at x^{+}, which already sits in a high-probability region, so a very short run produces a usable contrast.
(5) Truncation Trades Bias For Cost: per-update cost drops from a full mixing time to k transitions, at the cost of a systematically biased gradient that ignores model mass far from the data.
(6) CD Is An Update Rule, Not A True Gradient: the CD-1 update is not the exact gradient of any objective function, so convergence guarantees are weak and divergence is possible in principle.

Flow diagram with a top lane showing a data batch feeding the positive phase that lowers energy at the data point, and a bottom lane where the same data point initializes a Markov chain of k short steps producing the negative sample, with both phases feeding a single parameter update box

Figure 1: The two-phase update. Exact maximum likelihood would draw the negative sample from the model at equilibrium, costing a full chain run per parameter update. CD keeps the identical update form but seeds the chain at the data and stops after k steps, which turns an expensive unbiased gradient into a cheap biased one.

Mathematical Formulation:
p_{\theta}(x) = \frac{e^{-E_{\theta}(x)}}{Z(\theta)}
Z(\theta) = \int e^{-E_{\theta}(x)}\,dx
\log p_{\theta}(x) = -E_{\theta}(x) - \log Z(\theta)
\nabla_{\theta}\log Z(\theta) = -\mathbb{E}_{p_{\theta}}[\nabla_{\theta}E_{\theta}]
\nabla_{\theta}\mathcal{L} = \mathbb{E}_{p_{\mathrm{data}}}[\nabla_{\theta}E] - \mathbb{E}_{p_{\theta}}[\nabla_{\theta}E]

Where:

  • p_{\theta}(x) is the model density and E_{\theta}(x) \in \mathbb{R} the learned energy, with lower energy meaning higher probability.
  • Z(\theta) is the partition function, an integral over the full input space (a sum for discrete x) that depends on \theta and therefore cannot be ignored during training.
  • x is a configuration and \theta the parameters of the energy network.
  • \mathcal{L} = -\mathbb{E}_{p_{\mathrm{data}}}[\log p_{\theta}] is the average negative log-likelihood, so the first expectation is the positive phase and the second is the negative phase.
  • The identity \nabla_{\theta}\log Z = -\mathbb{E}_{p_{\theta}}[\nabla_{\theta}E] is what converts an intractable normalizer into an expectation, which is exactly why sampling becomes the bottleneck.

The Contrastive Divergence Approximation:
p_k = p_0 T_{\theta}^{k}
\nabla_{\theta}\mathcal{L}_{\mathrm{CD}} = \mathbb{E}_{p_0}[\nabla_{\theta}E] - \mathbb{E}_{p_k}[\nabla_{\theta}E]
\mathcal{L}_{\mathrm{CD}} \approx D_{KL}(p_0 \| p_{\theta}) - D_{KL}(p_k \| p_{\theta})

Where:

  • p_0 = p_{\mathrm{data}} is the empirical distribution used as the chain initialization, and p_k is the distribution after k applications of the transition operator.
  • T_{\theta} is any transition operator leaving p_{\theta} invariant, typically block Gibbs for an RBM or Langevin dynamics for a continuous EBM.
  • k is the truncation depth, with k = 1 the standard choice and larger k reducing bias at linear cost.
  • As k \to \infty we recover p_k = p_{\theta} and the update becomes the exact likelihood gradient, so CD is a controllable interpolation between a free estimate and an unbiased one.
  • The two-KL form is the original motivation rather than an exact objective; a small ignored term involving \partial p_k / \partial \theta is what makes CD not the gradient of any function.

The restricted Boltzmann machine is the case where every piece is closed-form, which is why CD was invented there. Because there are no visible-visible or hidden-hidden couplings, the conditionals factorize completely and one Gibbs sweep is a single pass of matrix multiplies plus sigmoids, following the chain v → h → v’ → h’. CD-1 then reduces to the difference between a data correlation and a reconstruction correlation, computable with two forward passes and no backpropagation through a sampler.

Binary RBM Case:
E_{\theta}(v,h) = -v^{\top}Wh - b^{\top}v - c^{\top}h
p(h_j = 1 \mid v) = \sigma(c_j + W_{:j}^{\top}v)
\Delta W = \eta\,(v_0 h_0^{\top} - v_1 h_1^{\top})

Here v_0 is the data vector, h_0 \sim p(h \mid v_0), and (v_1, h_1) come from one further Gibbs sweep. The positive term raises the weight between co-active data units and the negative term subtracts the same statistic measured on the model’s one-step reconstruction, so learning stops precisely when the reconstruction statistics match the data statistics.

One-dimensional energy landscape curve with a deep data basin on the left and a spurious model basin on the right separated by a barrier, showing the data point where the positive phase lowers energy, the k-step chain sample nearby where the negative phase raises energy, and a distant full-chain sample in the spurious basin that a short chain never reaches

Figure 2: The structural blind spot. A k-step chain started at x^{+} stays inside the data basin, so the negative phase only ever penalizes points the model already ranks near the data. A spurious mode behind an energy barrier keeps its low energy indefinitely because CD never samples there, and this is why CD-trained models can assign high likelihood to garbage.

The practical consequences follow directly from that picture. Slow mixing is not a nuisance but the defining constraint, since the time to cross a barrier grows exponentially in its height, so raising k from 1 to 20 buys much less than the cost suggests. Persistent CD attacks the same problem differently by keeping a set of chains alive across parameter updates instead of restarting them at data, which lets the fantasy particles drift into spurious regions given enough updates, at the cost of instability when the learning rate is large relative to the mixing rate. Modern continuous EBMs use short-run Langevin dynamics with a replay buffer plus gradient clipping and energy regularization, and increasingly avoid MCMC entirely through score matching or its denoising variant, which never touches Z(\theta) because the score \nabla_x \log p_{\theta}(x) = -\nabla_x E_{\theta}(x) is independent of the normalizer.

PropertyCD-kPersistent CDScore matching
Negative sample sourcek transitions from the current data batchPersistent fantasy particles carried across updatesNone; no sampling during training
Cost per updatek sweeps, usually oneOne sweep, plus buffer memoryOne or two extra backward passes for derivatives
Main bias or errorTruncation bias; distant spurious modes never penalizedChains lag a moving target, so samples are staleConsistent but blind to relative mass between separated modes
StabilityVery stable, low variance, forgiving of large learning ratesNeeds a small learning rate so chains can keep upStable, but plain score matching struggles in high dimension without noise
Best fitRBMs and discrete models with exact block GibbsLonger training runs where density quality matters more than speedContinuous high-dimensional data, the route taken by diffusion models

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *