Simple Algorithm
Deep Learning · Emergence · 28 min

Energy-Based Models

Unnormalized densities, contrastive divergence and score matching.

Write down any function of the input, call it an energy, and you have a probability model: pθ(x)∝e−Eθ(x)p_{\boldsymbol\theta}(\mathbf x)\propto e^{-E_{\boldsymbol\theta}(\mathbf x)}. The freedom is total, and so is the difficulty, because the normalizer is an integral over all of input space. This lesson derives what maximum likelihood then demands, the Markov chains that try to supply it and the biases they bring, and three ways to train without sampling at all: score matching, the denoising version that diffusion models are built on, and noise-contrastive estimation.

What you’ll learn

  • Derive the log-likelihood gradient of an energy-based model as the difference between the data and model expectations of ∇θEθ\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}.
  • Prove that e−Ee^{-E} is stationary for the Langevin diffusion, compute the bias of its unadjusted discretization, and explain why score-driven chains misjudge the weights of separated modes.
  • Explain contrastive divergence, persistent chains and the short-run samplers of deep EBMs, and what their biases do to the learned model.
  • Derive score matching by integration by parts and denoising score matching by the tower property, explain why noise-contrastive estimation is consistent, and read any classifier as an energy-based model.

The likelihood gradient and the missing samples

DerivationThe gradient of the log-likelihood
  1. log⁡pθ(x)=−Eθ(x)−log⁡Zθ\log p_{\boldsymbol\theta}(\mathbf x)=-E_{\boldsymbol\theta}(\mathbf x)-\log Z_{\boldsymbol\theta}

    The log-density is minus the energy, minus a constant that depends on the parameters:

  2. ∇θlog⁡Zθ=1Zθ∫∇θe−Eθ(x) dx=−∫e−Eθ(x)Zθ ∇θEθ(x) dx=−Epθ[∇θEθ]\nabla_{\boldsymbol\theta}\log Z_{\boldsymbol\theta}=\frac{1}{Z_{\boldsymbol\theta}}\int\nabla_{\boldsymbol\theta}e^{-E_{\boldsymbol\theta}(\mathbf x)}\,d\mathbf x=-\int\frac{e^{-E_{\boldsymbol\theta}(\mathbf x)}}{Z_{\boldsymbol\theta}}\,\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}(\mathbf x)\,d\mathbf x=-\E_{p_{\boldsymbol\theta}}\big[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}\big]

    Differentiate the log-partition function under the integral. The integrand turns into the model density itself:

  3. ∇θ 1n∑i=1nlog⁡pθ(xi)=−Ep^data[∇θEθ]+Epθ[∇θEθ]\nabla_{\boldsymbol\theta}\,\frac1n\sum_{i=1}^n\log p_{\boldsymbol\theta}(\mathbf x_i)=-\E_{\hat p_{\text{data}}}\big[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}\big]+\E_{p_{\boldsymbol\theta}}\big[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}\big]

    Average over the training set. The gradient of the log-likelihood is a difference of two expectations of the same quantity, under the data and under the model:

  4. Ascent lowers the energy of the data (the positive phase) and raises it wherever the model puts its mass (the negative phase). It stops when the two expectations agree; for an exponential family, Eθ(x)=−θ⊤T(x)E_{\boldsymbol\theta}(\mathbf x)=-\boldsymbol\theta^\top T(\mathbf x), that is the classic moment matching Edata[T]=Epθ[T]\E_{\text{data}}[T]=\E_{p_{\boldsymbol\theta}}[T].

Everything hinges on samples from pθp_{\boldsymbol\theta}, a distribution known only up to ZθZ_{\boldsymbol\theta} and changing with every update. Given exact samples x−\mathbf x^-, the stochastic gradient is unbiased, and it is the gradient of the surrogate loss Eθ(x+)−Eθ(x−)E_{\boldsymbol\theta}(\mathbf x^+)-E_{\boldsymbol\theta}(\mathbf x^-) with x+\mathbf x^+ a data point and x−\mathbf x^- held fixed, with no gradient through the sampler. That is how deep EBMs are implemented, and Du & Mordatch (2019) noted its resemblance to the critic objective of a Wasserstein GAN, with a Markov chain in place of a generator (Generative Adversarial Networks). The rest of this lesson is about where x−\mathbf x^- comes from, or how to do without it.

Langevin dynamics: sampling with the score

Since ∇xlog⁡pθ=−∇xEθ\nabla_{\mathbf x}\log p_{\boldsymbol\theta}=-\nabla_{\mathbf x}E_{\boldsymbol\theta} involves no normalizer, gradient-based Markov chains can sample an EBM. The basic one is the overdamped Langevin diffusion: gradient descent on the energy plus Brownian noise.

dxt=−∇E(xt) dt+2 dWt,xk+1=xk−η ∇E(xk)+2η ξk,ξk∼N(0,I)d\mathbf x_t=-\nabla E(\mathbf x_t)\,dt+\sqrt2\,d\mathbf W_t,\qquad\mathbf x_{k+1}=\mathbf x_k-\eta\,\nabla E(\mathbf x_k)+\sqrt{2\eta}\,\boldsymbol\xi_k,\quad\boldsymbol\xi_k\sim\mathcal N(\mathbf 0,I)
Left, the continuous-time diffusion; right, its Euler–Maruyama discretization with step η\eta, the unadjusted Langevin algorithm. Papers often write the same update with gradient step λ/2\lambda/2 and noise variance λ\lambda, that is, λ=2η\lambda=2\eta.
DerivationProof
  1. ∂tρt=∇⋅(ρt∇E)+Δρt=∇⋅(ρt∇E+∇ρt)\partial_t\rho_t=\nabla\cdot\big(\rho_t\nabla E\big)+\Delta\rho_t=\nabla\cdot\big(\rho_t\nabla E+\nabla\rho_t\big)

    The density ρt\rho_t of xt\mathbf x_t obeys the Fokker–Planck equation, a continuity equation whose flux has a drift part and a diffusion part:

  2. ρ∇E+∇ρ=ρ∇E−ρ∇E=0\rho\nabla E+\nabla\rho=\rho\nabla E-\rho\nabla E=\mathbf 0

    For ρ=e−E/Z\rho=e^{-E}/Z we have ∇ρ=−ρ∇E\nabla\rho=-\rho\nabla E, so the flux vanishes at every point, not just on average: the drift toward low energy exactly balances the spreading by noise. Hence ∂tρ=0\partial_t\rho=0. ■\blacksquare

Predict before you look

Run the unadjusted Langevin algorithm with step η=0.25\eta=0.25 on E(x)=x2/2E(x)=x^2/2, a standard normal. What is the variance of the chain's stationary distribution?

Show the answer

1.14

The update is xk+1=(1−η)xk+2η ξkx_{k+1}=(1-\eta)x_k+\sqrt{2\eta}\,\xi_k, so the stationary variance solves v=(1−η)2v+2ηv=(1-\eta)^2v+2\eta, giving v=2η1−(1−η)2=11−η/2=87≈1.14v=\frac{2\eta}{1-(1-\eta)^2}=\frac{1}{1-\eta/2}=\frac87\approx1.14: too wide by 14%, and with no stationary distribution at all for η≥2\eta\ge2. The bias vanishes only as η→0\eta\to0. A Metropolis–Hastings accept–reject step (the Metropolis-adjusted Langevin algorithm, MALA) removes it at any step size.

Separated modes. Langevin chains find mass by following local gradients, and that is their weakness on multimodal targets. Take p=πp1+(1−π)p2p=\pi p_1+(1-\pi)p_2 with components that barely overlap. Inside the support of p1p_1, ∇log⁡p≈∇log⁡(πp1)=∇log⁡p1\nabla\log p\approx\nabla\log(\pi p_1)=\nabla\log p_1: the score hardly depends on π\pi at all. To learn the weights a chain must cross the low-density gap, and for high energy barriers the expected crossing time grows exponentially with the barrier's height, roughly like eΔEe^{\Delta E} for a barrier of height ΔE\Delta E (the Eyring–Kramers law), so in practice the share of chains in each mode is set by where they started. Song & Ermon (2019) showed this with exact scores on a two-Gaussian mixture, and showed that annealing, running Langevin on the scores of less and less noised versions of the data, recovers the weights: under heavy noise the modes overlap, so the score depends on both of them and on their weights. That ladder of noise levels became the diffusion model (Diffusion Models).

Contrastive divergence and short-run samplers

Restricted Boltzmann machines made EBMs practical first. Binary visible units v\mathbf v and hidden units h\mathbf h interact only across the two layers, E(v,h)=−a⊤v−b⊤h−v⊤WhE(\mathbf v,\mathbf h)=-\mathbf a^\top\mathbf v-\mathbf b^\top\mathbf h-\mathbf v^\top W\mathbf h, so each layer is conditionally independent given the other: p(hj=1∣v)=σ(bj+v⊤W:,j)p(h_j=1\mid\mathbf v)=\sigma(b_j+\mathbf v^\top W_{:,j}) and p(vi=1∣h)=σ(ai+Wi,:h)p(v_i=1\mid\mathbf h)=\sigma(a_i+W_{i,:}\mathbf h), and block Gibbs sampling alternates between them. The likelihood gradient for WW is Edata[vh⊤]−Emodel[vh⊤]\E_{\text{data}}[\mathbf v\mathbf h^\top]-\E_{\text{model}}[\mathbf v\mathbf h^\top], with h\mathbf h drawn given v\mathbf v in the first term.

Contrastive divergence (CD-kk; Hinton, 2002) replaces the model expectation by the statistics after only kk Gibbs steps, often one, of a chain started at the training data:

ΔW∝⟨vh⊤⟩data−⟨vh⊤⟩k.\Delta W\propto\langle\mathbf v\mathbf h^\top\rangle_{\text{data}}-\langle\mathbf v\mathbf h^\top\rangle_{k}.

Hinton motivated it as approximately minimizing a difference of two divergences, DKL(p0 ∥ pθ)−DKL(pk ∥ pθ)\KL(p_0\,\|\,p_{\boldsymbol\theta})-\KL(p_k\,\|\,p_{\boldsymbol\theta}), where p0p_0 is the data distribution and pkp_k the distribution after kk steps, while dropping a term for how pkp_k itself moves with θ\boldsymbol\theta. The shortcut has costs. CD is biased: its fixed points generally differ from those of maximum likelihood, although the bias is typically small (Carreira-Perpiñán & Hinton, 2005). The CD-1 update for binary RBMs is not the gradient of any function, and with a suitably chosen regularizer CD learning can cycle forever (Sutskever & Tieleman, 2010). And because its chains start at the data and run briefly, CD rarely explores low-energy regions far from the data, so it does little to raise their energy.

Persistent CD (Tieleman, 2008) keeps a set of chains running across parameter updates instead of restarting them at the data; when the parameters change slowly, the chains stay close to the current model and can wander far from the data. Deep EBMs carried the recipe to images. Du & Mordatch (2019) ran Langevin dynamics on a convolutional energy, starting each chain from a replay buffer of earlier samples 95% of the time and from uniform noise otherwise, with spectral normalization and a small penalty on energy magnitudes for stability. Their models produced good samples, could be composed by adding energies, and detected out-of-distribution inputs. Their CIFAR-10 chains took 60 steps of size 10 with noise of standard deviation 0.005, hundreds of times smaller than a Langevin step of that size requires. Such samplers are noisy gradient descent, biased on purpose for speed.

Check your understanding

An EBM is trained with 60-step Langevin chains started from noise, with far less noise than Langevin requires, and the chains' samples look excellent. What does this establish?

  1. That e−Eθ/Zθe^{-E_{\boldsymbol\theta}}/Z_{\boldsymbol\theta} is an accurate density model of the data
  2. That the short-run sampler is a good generator; the density e−Eθe^{-E_{\boldsymbol\theta}} itself may put its mass elsewhere
  3. That the chains have mixed, since otherwise the samples would look poor
  4. Nothing: samples from an EBM are never informative
Show the answer

B. That the short-run sampler is a good generator; the density e−Eθe^{-E_{\boldsymbol\theta}} itself may put its mass elsewhere

Training matched the data to whatever the biased sampler produces, so sampler and data agree, but nothing forces long-run Langevin on EθE_{\boldsymbol\theta} to give the same distribution. Checking the energy itself takes long chains or a likelihood estimate.

Training without sampling

Sampling inside the training loop can be avoided by matching something other than the likelihood. The Fisher divergence compares scores, which do not involve ZθZ_{\boldsymbol\theta}. With the model's score sθ(x)=−∇xEθ(x)\mathbf s_{\boldsymbol\theta}(\mathbf x)=-\nabla_{\mathbf x}E_{\boldsymbol\theta}(\mathbf x),

J(θ)=12 Epdata∥sθ(x)−∇xlog⁡pdata(x)∥2.J(\boldsymbol\theta)=\tfrac12\,\E_{p_{\text{data}}}\big\lVert\mathbf s_{\boldsymbol\theta}(\mathbf x)-\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x)\big\rVert^2.

Two log-densities with the same gradient everywhere differ by a constant, and normalization forces the constant to be zero, so J=0J=0 only when the model matches the data. The data's score is unknown, but it can be eliminated.

DerivationProof in one dimension
  1. J=12E[sθ(x)2]−E[sθ(x) (log⁡p)′(x)]+12E[(log⁡p)′(x)2]J=\tfrac12\E\big[s_{\boldsymbol\theta}(x)^2\big]-\E\big[s_{\boldsymbol\theta}(x)\,(\log p)'(x)\big]+\tfrac12\E\big[(\log p)'(x)^2\big]

    Expand the square. The last term involves only the data and is the constant CC:

  2. E[sθ (log⁡p)′]=∫p(x) p′(x)p(x) sθ(x) dx=∫p′(x) sθ(x) dx\E\big[s_{\boldsymbol\theta}\,(\log p)'\big]=\int p(x)\,\frac{p'(x)}{p(x)}\,s_{\boldsymbol\theta}(x)\,dx=\int p'(x)\,s_{\boldsymbol\theta}(x)\,dx

    In the cross term, the density cancels against the derivative of its logarithm, p (log⁡p)′=p′p\,(\log p)'=p':

  3. ∫p′ sθ dx=[p sθ]−∞∞−∫p sθ′ dx=−E[sθ′(x)]\int p'\,s_{\boldsymbol\theta}\,dx=\Big[p\,s_{\boldsymbol\theta}\Big]_{-\infty}^{\infty}-\int p\,s_{\boldsymbol\theta}'\,dx=-\E\big[s_{\boldsymbol\theta}'(x)\big]

    Integrate by parts. The boundary term vanishes by the decay assumption, and what remains is an expectation under the data of something the model can compute:

  4. So J=E[sθ′(x)+12sθ(x)2]+CJ=\E\big[s_{\boldsymbol\theta}'(x)+\tfrac12s_{\boldsymbol\theta}(x)^2\big]+C. In dd dimensions the same step, applied coordinate by coordinate, gives the trace. ■\blacksquare Under further identifiability conditions Hyvärinen also proved that minimizing the sample version of JJ is a consistent estimator.

The trace is the bottleneck. tr⁡∇xsθ\operatorname{tr}\nabla_{\mathbf x}\mathbf s_{\boldsymbol\theta} needs the diagonal of a d×dd\times d matrix of second derivatives of the energy, about dd backward passes. Sliced score matching (Song, Garg, Shi & Ermon, 2019) projects onto random directions, Ev[v⊤∇xsθ v]\E_{\mathbf v}[\mathbf v^\top\nabla_{\mathbf x}\mathbf s_{\boldsymbol\theta}\,\mathbf v], the trick of Hutchinson's estimator in Continuous Flows & Flow Matching. Denoising score matching (Vincent, 2011) removes the trace altogether by changing the target: corrupt each data point, x~=x+σϵ\tilde{\mathbf x}=\mathbf x+\sigma\boldsymbol\epsilon, and fit the score of the corrupted density qσ(x~)=∫qσ(x~∣x) pdata(x) dxq_\sigma(\tilde{\mathbf x})=\int q_\sigma(\tilde{\mathbf x}\mid\mathbf x)\,p_{\text{data}}(\mathbf x)\,d\mathbf x.

DerivationProof
  1. ∇log⁡qσ(x~)=∫qσ(x~∣x) pdata(x)qσ(x~) ∇x~log⁡qσ(x~∣x) dx=E[∇x~log⁡qσ(x~∣x) ∣ x~]\nabla\log q_\sigma(\tilde{\mathbf x})=\int\frac{q_\sigma(\tilde{\mathbf x}\mid\mathbf x)\,p_{\text{data}}(\mathbf x)}{q_\sigma(\tilde{\mathbf x})}\,\nabla_{\tilde{\mathbf x}}\log q_\sigma(\tilde{\mathbf x}\mid\mathbf x)\,d\mathbf x=\E\big[\nabla_{\tilde{\mathbf x}}\log q_\sigma(\tilde{\mathbf x}\mid\mathbf x)\,\big|\,\tilde{\mathbf x}\big]

    Differentiate the mixture qσq_\sigma under the integral and divide by it. The score of the mixture is the posterior average of the components' scores:

  2. Expand both squares. The ∥sθ∥2\norm{\mathbf s_{\boldsymbol\theta}}^2 terms agree, the cross terms agree by the tower property, and the rest does not involve θ\boldsymbol\theta. ■\blacksquare This is the argument of conditional flow matching, with a score in place of a velocity.

The price is that the model learns the score of the noised data, which approaches the data score only as σ→0\sigma\to0, where the target −ϵ/σ-\boldsymbol\epsilon/\sigma becomes very noisy. Learning it at many noise levels at once and sampling by descending through them is how diffusion models work; Diffusion Models shows that their noise-prediction loss is this objective at every noise level, up to a weight per level.

Noise-contrastive estimation (NCE; Gutmann & Hyvärinen, 2010) keeps the energy and replaces sampling by classification. Treat the log-normalizer as one more parameter cc, so log⁡pm(x)=−Eθ(x)−c\log p_m(\mathbf x)=-E_{\boldsymbol\theta}(\mathbf x)-c; draw ν\nu noise samples per data point from a density pnp_n that can be evaluated; and train a logistic classifier to tell data from noise with the logit

G(x)=log⁡pm(x)−log⁡(ν pn(x)).G(\mathbf x)=\log p_m(\mathbf x)-\log\big(\nu\,p_n(\mathbf x)\big).

The Bayes-optimal classifier has logit log⁡pdata−log⁡(νpn)\log p_{\text{data}}-\log(\nu p_n), the density-ratio fact behind the optimal GAN discriminator (Generative Adversarial Networks). If the model family contains the data distribution and pn>0p_n>0 wherever the data are, the optimum therefore has pm=pdatap_m=p_{\text{data}}, which is normalized, so cc converges to log⁡Zθ\log Z_{\boldsymbol\theta}; Gutmann & Hyvärinen proved the estimator consistent. NCE works best when the noise resembles the data, since an easy discrimination gives little signal (Song & Kingma, 2021, review variants that adapt the noise). Contrastive representation learning borrowed the idea and the name (Self-Supervised Learning).

Classifiers are energy-based models

Any classifier with logits fθ(x)∈RKf_{\boldsymbol\theta}(\mathbf x)\in\R^K already defines an EBM over inputs and labels. Grathwohl et al. (2020) called the result a joint energy-based model (JEM):

pθ(x,y)=efθ(x)[y]Zθ,pθ(y∣x)=efθ(x)[y]∑y′efθ(x)[y′],Eθ(x)=−log⁡∑yefθ(x)[y]p_{\boldsymbol\theta}(\mathbf x,y)=\frac{e^{f_{\boldsymbol\theta}(\mathbf x)[y]}}{Z_{\boldsymbol\theta}},\qquad p_{\boldsymbol\theta}(y\mid\mathbf x)=\frac{e^{f_{\boldsymbol\theta}(\mathbf x)[y]}}{\sum_{y'}e^{f_{\boldsymbol\theta}(\mathbf x)[y']}},\qquad E_{\boldsymbol\theta}(\mathbf x)=-\log\sum_ye^{f_{\boldsymbol\theta}(\mathbf x)[y]}
The conditional is the usual softmax classifier, since ZθZ_{\boldsymbol\theta} cancels. Summing out yy gives an unnormalized density over inputs, whose energy is minus the log-sum-exp of the logits. Shifting all the logits at x\mathbf x by a constant leaves the classifier unchanged but changes pθ(x)p_{\boldsymbol\theta}(\mathbf x): the density lives in the one degree of freedom a classifier ignores.

JEM trained this model on log⁡pθ(y∣x)+log⁡pθ(x)\log p_{\boldsymbol\theta}(y\mid\mathbf x)+\log p_{\boldsymbol\theta}(\mathbf x): the first term by ordinary cross-entropy, the second by the likelihood gradient with short-run sampling. Its chains took 20 steps from a replay buffer, reinitialized from noise with probability 5%, with step size 1 and noise of standard deviation 0.01, again about a hundred times less noise than Langevin requires. The authors reported better calibration, out-of-distribution detection and adversarial robustness than comparable classifiers, and samples rivaling contemporary GANs. They were candid about the cost: the gradient estimates were unstable, and their models "regularly diverged throughout training", needing restarts with lower learning rates or more regularization.

Check your understanding

In a JEM, add the same constant c>0c>0 to all KK logits at one input x\mathbf x. What changes?

  1. The predicted class probabilities pθ(y∣x)p_{\boldsymbol\theta}(y\mid\mathbf x)
  2. Nothing at all
  3. The energy Eθ(x)E_{\boldsymbol\theta}(\mathbf x) falls by cc, so the unnormalized density at x\mathbf x grows by the factor ece^c, while pθ(y∣x)p_{\boldsymbol\theta}(y\mid\mathbf x) is unchanged
  4. Every class probability grows by the factor ece^c
Show the answer

C. The energy Eθ(x)E_{\boldsymbol\theta}(\mathbf x) falls by cc, so the unnormalized density at x\mathbf x grows by the factor ece^c, while pθ(y∣x)p_{\boldsymbol\theta}(y\mid\mathbf x) is unchanged

log⁡∑yef[y]+c=c+log⁡∑yef[y]\log\sum_ye^{f[y]+c}=c+\log\sum_ye^{f[y]}, so Eθ(x)E_{\boldsymbol\theta}(\mathbf x) drops by cc. The softmax is invariant to a common shift, which is why an ordinary classifier never pins this direction down and JEM can spend it on density.

Key takeaways

  • An EBM's log-likelihood gradient is −Edata[∇θEθ]+Epθ[∇θEθ]-\E_{\text{data}}[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}]+\E_{p_{\boldsymbol\theta}}[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}]: push the energy down on data and up on model samples, which must come from MCMC.
  • The Langevin diffusion leaves e−Ee^{-E} invariant because its probability flux vanishes; the unadjusted algorithm is biased (stationary variance 1/(1−η/2)1/(1-\eta/2) for a standard normal), and score-driven chains cannot learn the weights of separated modes in reasonable time, which annealing through noise levels repairs.
  • Contrastive divergence starts short chains at the data: biased, and for binary RBMs not the gradient of any function. Persistent chains and replay buffers keep chains running; with the short, low-noise samplers of deep EBMs, the sampler rather than e−Eθe^{-E_{\boldsymbol\theta}} may be what matches the data.
  • Score matching turns the Fisher divergence into E[tr⁡∇sθ+12∥sθ∥2]\E[\operatorname{tr}\nabla\mathbf s_{\boldsymbol\theta}+\tfrac12\norm{\mathbf s_{\boldsymbol\theta}}^2] by integration by parts; denoising score matching regresses on −ϵ/σ-\boldsymbol\epsilon/\sigma and learns the noised score, the objective of diffusion models; NCE classifies data against noise with log⁡Zθ\log Z_{\boldsymbol\theta} as a parameter.
  • A classifier's logits define an EBM with E(x)=−log⁡∑yef(x)[y]E(\mathbf x)=-\log\sum_ye^{f(\mathbf x)[y]}; trained as one (JEM), it gained calibration and out-of-distribution detection at the price of unstable training.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In the log-likelihood gradient −Edata[∇θEθ]+Epθ[∇θEθ]-\E_{\text{data}}[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}]+\E_{p_{\boldsymbol\theta}}[\nabla_{\boldsymbol\theta}E_{\boldsymbol\theta}] of an EBM, what does the second term do during gradient ascent, and why is it hard to compute?

  1. It lowers the energy of the data; it is hard because the data set is large
  2. It raises the energy wherever the model currently puts its mass, and it needs samples from the model, which is known only up to ZθZ_{\boldsymbol\theta}
  3. It normalizes the density exactly at every step
  4. It is a regularizer that can be dropped once training has converged
Show the answer

B. It raises the energy wherever the model currently puts its mass, and it needs samples from the model, which is known only up to ZθZ_{\boldsymbol\theta}

The second term is ∇θ\nabla_{\boldsymbol\theta} of −log⁡Zθ-\log Z_{\boldsymbol\theta}, an expectation under pθp_{\boldsymbol\theta}. It pushes the energy up on the model's own samples, which keeps the density from piling up away from the data. Estimating it takes samples from pθp_{\boldsymbol\theta}, usually by MCMC.

Question 2

Langevin dynamics with the exact score is run on a mixture of two well-separated Gaussians with weights 0.7 and 0.3, from chains started uniformly over a region containing both. After a moderate number of steps, what fraction of the chains sits in the heavier mode?

  1. About 0.7, because the score encodes the weights
  2. About 1, because chains drift toward the heavier mode
  3. Roughly the fraction that started in its basin of attraction: the local score does not depend on the weights, and crossing the gap takes exponentially long
  4. About 0.5, because Langevin dynamics treats modes symmetrically
Show the answer

C. Roughly the fraction that started in its basin of attraction: the local score does not depend on the weights, and crossing the gap takes exponentially long

Inside a mode's support, ∇log⁡(πp1+(1−π)p2)≈∇log⁡p1\nabla\log(\pi p_1+(1-\pi)p_2)\approx\nabla\log p_1, whatever π\pi is. Chains settle in the basin they start in, so the split reflects the initialization until the chain mixes, which takes a time exponential in the barrier height. Annealing through noise levels (Song & Ermon, 2019) repairs this.

Question 3

Denoising score matching with Gaussian noise of standard deviation σ\sigma regresses sθ(x~)\mathbf s_{\boldsymbol\theta}(\tilde{\mathbf x}) on −(x~−x)/σ2-(\tilde{\mathbf x}-\mathbf x)/\sigma^2. At the optimum, sθ\mathbf s_{\boldsymbol\theta} equals:

  1. The score of the noised data, ∇log⁡qσ\nabla\log q_\sigma, which approaches the data's score only as σ→0\sigma\to0
  2. The score of the data, ∇log⁡pdata\nabla\log p_{\text{data}}, for any σ\sigma
  3. The target −(x~−x)/σ2-(\tilde{\mathbf x}-\mathbf x)/\sigma^2 of each training pair, which it memorizes
  4. Minus the energy, −Eθ-E_{\boldsymbol\theta}
Show the answer

A. The score of the noised data, ∇log⁡qσ\nabla\log q_\sigma, which approaches the data's score only as σ→0\sigma\to0

The regression's minimizer is the conditional mean of the target given x~\tilde{\mathbf x}, and that posterior average of the components' scores is exactly ∇log⁡qσ(x~)\nabla\log q_\sigma(\tilde{\mathbf x}). Diffusion models exploit this at every noise level instead of trying to make σ\sigma tiny.

Question 4

In noise-contrastive estimation the log-normalizer is a free parameter cc, with log⁡pm=−Eθ−c\log p_m=-E_{\boldsymbol\theta}-c. If the model family contains the data distribution, what does cc converge to?

  1. 00
  2. log⁡ν\log\nu, the log of the noise-to-data ratio
  3. The entropy of the noise distribution
  4. log⁡Zθ\log Z_{\boldsymbol\theta}, so that pmp_m is normalized
Show the answer

D. log⁡Zθ\log Z_{\boldsymbol\theta}, so that pmp_m is normalized

The optimal classifier needs log⁡pm−log⁡(νpn)=log⁡pdata−log⁡(νpn)\log p_m-\log(\nu p_n)=\log p_{\text{data}}-\log(\nu p_n), hence pm=pdatap_m=p_{\text{data}}, a normalized density. With pm=e−Eθ−cp_m=e^{-E_{\boldsymbol\theta}-c} that forces c=log⁡∫e−Eθ=log⁡Zθc=\log\int e^{-E_{\boldsymbol\theta}}=\log Z_{\boldsymbol\theta}.

Further reading

  • G. E. Hinton, "Training products of experts by minimizing contrastive divergence," Neural Computation 14(8), 1771–1800 (2002).
  • A. Hyvärinen, "Estimation of non-normalized statistical models by score matching," Journal of Machine Learning Research 6, 695–709 (2005).
  • P. Vincent, "A connection between score matching and denoising autoencoders," Neural Computation 23(7), 1661–1674 (2011).
  • M. Gutmann & A. Hyvärinen, "Noise-contrastive estimation: A new estimation principle for unnormalized statistical models," AISTATS (2010).
  • Y. Song & D. P. Kingma, "How to train your energy-based models," arXiv:2101.03288 (2021).