Foundation reference

Score Matching & Score-Based Generative Models

Score functions underlie diffusion models: the network trained at each noise level is, up to scaling, an estimate of the score of the noised data distribution (Song & Ermon, 2019).

A newer notebook covers this concept. This older page keeps its summary, math and sources.

Concept 36 of 100Generative ModelsStudy phase 4
Key equation
s(x)=∇xlog⁡p(x)s(x) = \nabla_x \log p(x)
Reading map and next steps

Study guide

Where to start

Concept 36 of 100Score MatchingGenerative Models · Study phase 4: Generative modeling families
Question

Why does the score point toward regions of higher density, and how does Langevin dynamics use it, together with added noise, to turn noise into samples?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Before you use the demo in the newer notebook, say what you expect to change.

CheckCheck against the math and the papers.

Compare what the notebook demo shows with the key equation here and with the papers cited below.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueContinue in the newer notebook.

The newer notebook covers the same concept and has an interactive demo.

Why It Matters for Modern Models

  • Score functions underlie diffusion models: the network trained at each noise level is, up to scaling, an estimate of the score of the noised data distribution (Song & Ermon, 2019).
  • This is why diffusion training is a regression problem: the best possible noise prediction is −σ ∇x~log⁡qσ(x~)-\sigma\, \nabla_{\tilde x} \log q_\sigma(\tilde x), a rescaled score.
  • Energy-based models and diffusion models meet here: both can be trained through the score, which does not depend on the intractable normalizing constant.

Questions to Think About

  • Why does a denoiser that predicts E[x∣x~]\mathbb E[x \mid \tilde x] give the score through Tweedie's formula, ∇log⁡qσ(x~)=(E[x∣x~]−x~)/σ2\nabla \log q_\sigma(\tilde x) = (\mathbb E[x \mid \tilde x] - \tilde x)/\sigma^2?
  • Why can score matching avoid the partition function entirely, and what does a model trained only on gradients of log p fail to tell you about p itself?

Interactive Demo

The interactive demo for this concept is in the newer notebook.

Open the notebook demo

Core Math

If you prefer intuition first, try the demo in the newer notebook, then come back for the math.

Key Equation
s(x)=∇xlog⁡p(x)s(x) = \nabla_x \log p(x)

The score of a density is the gradient of its logarithm with respect to the input:

s(x)=∇xlog⁡p(x)s(x) = \nabla_x \log p(x)

If p(x)=e−E(x)/Zp(x) = e^{-E(x)}/Z, the score is −∇xE(x)-\nabla_x E(x): the normalizing constant ZZ drops out.

Score matching fits a model sθ(x)≈∇xlog⁡pdata(x)s_\theta(x) \approx \nabla_x \log p_{\text{data}}(x) without knowing ZZ or the true score. After integration by parts, the objective needs only samples from the data (Hyvärinen, 2005):

LSM=Epdata[12∥sθ(x)∥2+tr(∇xsθ(x))]\mathcal{L}_{SM} = \mathbb{E}_{p_{\text{data}}}\left[ \frac{1}{2}\|s_\theta(x)\|^2 + \text{tr}(\nabla_x s_\theta(x)) \right]

The trace term is expensive in high dimensions, which motivates the denoising form.

Denoising score matching adds Gaussian noise, x~=x+σϵ\tilde{x} = x + \sigma\epsilon with ϵ∼N(0,I)\epsilon \sim \mathcal N(0, I), and regresses onto the score of the noise kernel (Vincent, 2011):

LDSM=Ex,x~[∥sθ(x~)−∇x~log⁡q(x~∣x)∥2],∇x~log⁡q(x~∣x)=−x~−xσ2=−ϵσ\mathcal{L}_{DSM} = \mathbb{E}_{x, \tilde{x}}\left[ \|s_\theta(\tilde{x}) - \nabla_{\tilde{x}} \log q(\tilde{x}|x)\|^2 \right], \qquad \nabla_{\tilde{x}} \log q(\tilde{x}|x) = -\frac{\tilde x - x}{\sigma^2} = -\frac{\epsilon}{\sigma}

The regression target −ϵ/σ-\epsilon/\sigma is noisy, but the minimizer is its conditional mean given x~\tilde x, which equals the score of the noised data distribution, ∇x~log⁡qσ(x~)\nabla_{\tilde x} \log q_\sigma(\tilde x).

Tweedie's formula connects this to denoising (Efron, 2011):

E[x∣x~]=x~+σ2 ∇x~log⁡qσ(x~)\mathbb E[x \mid \tilde x] = \tilde x + \sigma^2\, \nabla_{\tilde x} \log q_\sigma(\tilde x)

so the best denoiser and the score of the noised distribution determine each other.

Key Papers

Estimation of Non-Normalized Statistical Models by Score Matching

Hyvärinen2005JMLR
Read paper →

A Connection Between Score Matching and Denoising Autoencoders

Vincent2011Neural Computation
Read paper →

Generative Modeling by Estimating Gradients of the Data Distribution

Song & Ermon2019NeurIPS
Read paper →

Tweedie's Formula and Selection Bias

Efron2011Journal of the American Statistical Association
Read paper →

Connections

Where to go next

A newer notebook covers Score Matching, with an interactive demo.

Continue in the newer notebook