Score Matching & Score-Based Models

The score function, denoising score matching, Stein's identity, connection between score-based and diffusion models, Langevin dynamics sampling, and noise-conditional score networks.

Advanced

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Score Function
  5. Why Scores Instead of Densities?
  6. Explicit Score Matching
  7. Denoising Score Matching
  8. Langevin Dynamics Sampling
  9. Noise-Conditional Score Networks (NCSN)
  10. Connection to Diffusion Models
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Define the score function and explain why it avoids the normalization constant.
  2. Derive denoising score matching from first principles.
  3. Explain Langevin dynamics and prove it converges to the target distribution.
  4. Connect score-based models to DDPM through the noise prediction equivalence.
  5. Explain why multi-scale noise is needed for effective score estimation.

Notation

  • xlogp(x)\nabla_{\mathbf{x}} \log p(\mathbf{x}) — score function
  • sθ(x,σ)\mathbf{s}_\theta(\mathbf{x}, \sigma) — learned score network
  • σ1>σ2>>σL\sigma_1 > \sigma_2 > \ldots > \sigma_L — noise schedule
  • ϵ\epsilon — step size for Langevin dynamics

Core Intuition

We want to learn a generative model of data pdata(x)p_{\text{data}}(\mathbf{x}), but directly estimating the density requires computing an intractable normalization constant Z=p(x)dxZ = \int p(\mathbf{x})d\mathbf{x}. The score function xlogp(x)\nabla_\mathbf{x}\log p(\mathbf{x}) is the gradient of the log-density — it tells us the direction of increasing probability at any point, and crucially, it doesn't depend on ZZ.

Score Function ∇log p(x)

Noise σ
0.80
Data modesScore vectors
Explore: The score ∇log p(x) points uphill in log-density toward data modes. Higher noise smooths the score field; denoising score matching trains a network to predict this at each noise level.

The Score Function

s(x)xlogp(x).(1)\mathbf{s}(\mathbf{x}) \triangleq \nabla_\mathbf{x} \log p(\mathbf{x}). \tag{1}

Properties:

  • Points toward modes of the distribution.
  • Magnitude indicates steepness of the density landscape.
  • Independent of normalization: xlogp(x)Z=xlogp(x)\nabla_\mathbf{x}\log\frac{p^*(\mathbf{x})}{Z} = \nabla_\mathbf{x}\log p^*(\mathbf{x}).

For Gaussian p(x)=N(μ,σ2I)p(\mathbf{x}) = \mathcal{N}(\boldsymbol{\mu}, \sigma^2\mathbf{I}):

s(x)=xμσ2.(2)\mathbf{s}(\mathbf{x}) = -\frac{\mathbf{x} - \boldsymbol{\mu}}{\sigma^2}. \tag{2}

The score points from x\mathbf{x} toward the mean μ\boldsymbol{\mu}, scaled by 1/σ21/\sigma^2.


Why Scores Instead of Densities?

Problem with density estimation: For energy-based models pθ(x)=eEθ(x)/Zθp_\theta(\mathbf{x}) = e^{-E_\theta(\mathbf{x})}/Z_\theta, computing Zθ=eEθ(x)dxZ_\theta = \int e^{-E_\theta(\mathbf{x})}d\mathbf{x} is intractable in high dimensions.

Score bypasses ZZ: Training a score model sθ(x)xlogp(x)\mathbf{s}_\theta(\mathbf{x}) \approx \nabla_\mathbf{x}\log p(\mathbf{x}) requires no normalization constant.

Sampling with scores: Once we have the score, we can generate samples using Langevin dynamics — following the score uphill with noise injection.


Explicit Score Matching

Objective (Fisher divergence):

LESM=12Epdata[sθ(x)xlogpdata(x)2].(3)\mathcal{L}_{\text{ESM}} = \frac{1}{2}\mathbb{E}_{p_{\text{data}}}\left[\|\mathbf{s}_\theta(\mathbf{x}) - \nabla_\mathbf{x}\log p_{\text{data}}(\mathbf{x})\|^2\right]. \tag{3}

Problem: We don't know xlogpdata(x)\nabla_\mathbf{x}\log p_{\text{data}}(\mathbf{x}) — that's what we're trying to learn!

Hyvärinen's trick: Using integration by parts (Stein's identity):

LESM=Epdata[tr(xsθ(x))+12sθ(x)2]+C.(4)\mathcal{L}_{\text{ESM}} = \mathbb{E}_{p_{\text{data}}}\left[\text{tr}(\nabla_\mathbf{x}\mathbf{s}_\theta(\mathbf{x})) + \frac{1}{2}\|\mathbf{s}_\theta(\mathbf{x})\|^2\right] + C. \tag{4}

This only involves the model sθ\mathbf{s}_\theta and data samples — no ground-truth score needed. But computing tr(xsθ)\text{tr}(\nabla_\mathbf{x}\mathbf{s}_\theta) (the Jacobian trace) is expensive for high-dimensional x\mathbf{x}.


Denoising Score Matching

Key insight (Vincent, 2011): Instead of matching the data score, match the score of noisy data.

Perturb data: x~=x+σϵ\tilde{\mathbf{x}} = \mathbf{x} + \sigma\boldsymbol{\epsilon}, where ϵN(0,I)\boldsymbol{\epsilon} \sim \mathcal{N}(0, \mathbf{I}).

The score of the noisy distribution has a simple form:

x~logpσ(x~x)=x~xσ2=ϵσ.(5)\nabla_{\tilde{\mathbf{x}}} \log p_\sigma(\tilde{\mathbf{x}} | \mathbf{x}) = -\frac{\tilde{\mathbf{x}} - \mathbf{x}}{\sigma^2} = -\frac{\boldsymbol{\epsilon}}{\sigma}. \tag{5}

Denoising score matching objective:

LDSM=ExpdataEϵN(0,I)[sθ(x+σϵ,σ)+ϵσ2].(6)\mathcal{L}_{\text{DSM}} = \mathbb{E}_{\mathbf{x} \sim p_{\text{data}}}\mathbb{E}_{\boldsymbol{\epsilon} \sim \mathcal{N}(0,\mathbf{I})}\left[\left\|\mathbf{s}_\theta(\mathbf{x} + \sigma\boldsymbol{\epsilon}, \sigma) + \frac{\boldsymbol{\epsilon}}{\sigma}\right\|^2\right]. \tag{6}

Equivalence: Minimizing DSM is equivalent to minimizing ESM for the noisy distribution. As σ0\sigma \to 0, we recover the true data score.


Langevin Dynamics Sampling

Given the score sθ(x)\mathbf{s}_\theta(\mathbf{x}), generate samples via:

xt+1=xt+ϵ2sθ(xt)+ϵzt,ztN(0,I).(7)\mathbf{x}_{t+1} = \mathbf{x}_t + \frac{\epsilon}{2}\mathbf{s}_\theta(\mathbf{x}_t) + \sqrt{\epsilon}\,\mathbf{z}_t, \quad \mathbf{z}_t \sim \mathcal{N}(0, \mathbf{I}). \tag{7}

Theorem: As ϵ0\epsilon \to 0 and tt \to \infty, xt\mathbf{x}_t converges to a sample from p(x)p(\mathbf{x}) (under mild regularity conditions).

Intuition: The score term pushes x\mathbf{x} toward high-density regions; the noise term ensures exploration and prevents collapse to modes.


Noise-Conditional Score Networks (NCSN)

Problem with single-noise DSM: At low noise, the score is inaccurate in low-density regions (no training data there). At high noise, the score is accurate everywhere but blurs fine details.

Solution (Song & Ermon, 2019): Train a single network on multiple noise levels:

LNCSN=i=1Lλ(σi)E[sθ(x~,σi)+ϵσi2],(8)\mathcal{L}_{\text{NCSN}} = \sum_{i=1}^L \lambda(\sigma_i)\,\mathbb{E}\left[\left\|\mathbf{s}_\theta(\tilde{\mathbf{x}}, \sigma_i) + \frac{\boldsymbol{\epsilon}}{\sigma_i}\right\|^2\right], \tag{8}

with σ1>>σL\sigma_1 > \ldots > \sigma_L and λ(σi)=σi2\lambda(\sigma_i) = \sigma_i^2 (normalization).

Annealed Langevin dynamics: Sample by running Langevin at each noise level sequentially, from largest σ1\sigma_1 (coarse structure) to smallest σL\sigma_L (fine details).


Connection to Diffusion Models

Theorem (Song et al., 2021): DDPM's noise prediction ϵθ(xt,t)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) and the score function are related by:

sθ(xt,t)=ϵθ(xt,t)1αˉt.(9)\mathbf{s}_\theta(\mathbf{x}_t, t) = -\frac{\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)}{\sqrt{1 - \bar{\alpha}_t}}. \tag{9}

The DDPM training objective (predict noise) is exactly denoising score matching (up to a weighting factor).

Unified view: Both diffusion models and score-based models learn the score at different noise levels. They differ only in how they use the score for sampling (discrete steps vs Langevin vs SDEs).


Common Pitfalls

Pitfall 1. Training a score network without noise conditioning. In low-density regions, the score is poorly estimated, causing Langevin dynamics to diverge.

Pitfall 2. Using too few Langevin steps per noise level. Insufficient mixing leads to poor sample quality, especially at lower noise levels.

Pitfall 3. Confusing the score with the gradient of the loss. The score xlogp(x)\nabla_\mathbf{x}\log p(\mathbf{x}) is a function of the data point, not the model parameters.


Summary

  • The score function xlogp(x)\nabla_\mathbf{x}\log p(\mathbf{x}) avoids the intractable normalization constant.
  • Denoising score matching provides a tractable training objective: predict the noise direction.
  • Langevin dynamics converts scores into samples by gradient ascent with noise.
  • Multi-scale noise (NCSN) ensures accurate scores everywhere.
  • DDPM noise prediction = denoising score matching (equivalent formulations).

Exercises

Exercise 1. Compute the score function for a mixture of two Gaussians p(x)=12N(μ1,σ2)+12N(μ2,σ2)p(\mathbf{x}) = \frac{1}{2}\mathcal{N}(\mu_1, \sigma^2) + \frac{1}{2}\mathcal{N}(\mu_2, \sigma^2).

Exercise 2. Prove that denoising score matching (equation 6) is equivalent to explicit score matching for the noisy distribution.

Exercise 3. Derive the stationary distribution of Langevin dynamics (equation 7) using the Fokker-Planck equation.

Exercise 4. Show that sθ(xt,t)=ϵθ/1αˉt\mathbf{s}_\theta(\mathbf{x}_t, t) = -\boldsymbol{\epsilon}_\theta/\sqrt{1-\bar{\alpha}_t} by differentiating the DDPM forward process.

Exercise 5. Explain why λ(σi)=σi2\lambda(\sigma_i) = \sigma_i^2 is the appropriate weighting in the NCSN objective.