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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Score Function
- Why Scores Instead of Densities?
- Explicit Score Matching
- Denoising Score Matching
- Langevin Dynamics Sampling
- Noise-Conditional Score Networks (NCSN)
- Connection to Diffusion Models
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Define the score function and explain why it avoids the normalization constant.
- Derive denoising score matching from first principles.
- Explain Langevin dynamics and prove it converges to the target distribution.
- Connect score-based models to DDPM through the noise prediction equivalence.
- Explain why multi-scale noise is needed for effective score estimation.
Notation
- — score function
- — learned score network
- — noise schedule
- — step size for Langevin dynamics
Core Intuition
We want to learn a generative model of data , but directly estimating the density requires computing an intractable normalization constant . The score function 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 .
Score Function ∇log p(x)
The Score Function
Properties:
- Points toward modes of the distribution.
- Magnitude indicates steepness of the density landscape.
- Independent of normalization: .
For Gaussian :
The score points from toward the mean , scaled by .
Why Scores Instead of Densities?
Problem with density estimation: For energy-based models , computing is intractable in high dimensions.
Score bypasses : Training a score model 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):
Problem: We don't know — that's what we're trying to learn!
Hyvärinen's trick: Using integration by parts (Stein's identity):
This only involves the model and data samples — no ground-truth score needed. But computing (the Jacobian trace) is expensive for high-dimensional .
Denoising Score Matching
Key insight (Vincent, 2011): Instead of matching the data score, match the score of noisy data.
Perturb data: , where .
The score of the noisy distribution has a simple form:
Denoising score matching objective:
Equivalence: Minimizing DSM is equivalent to minimizing ESM for the noisy distribution. As , we recover the true data score.
Langevin Dynamics Sampling
Given the score , generate samples via:
Theorem: As and , converges to a sample from (under mild regularity conditions).
Intuition: The score term pushes 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:
with and (normalization).
Annealed Langevin dynamics: Sample by running Langevin at each noise level sequentially, from largest (coarse structure) to smallest (fine details).
Connection to Diffusion Models
Theorem (Song et al., 2021): DDPM's noise prediction and the score function are related by:
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 is a function of the data point, not the model parameters.
Summary
- The score function 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 .
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 by differentiating the DDPM forward process.
Exercise 5. Explain why is the appropriate weighting in the NCSN objective.