Search

Search papers and lessons

Generative Modeling by Estimating Gradients of the Data Distribution

Yang Song, Stefano Ermon · 2019

Song and Ermon (2019) make images by learning where data lies at many noise levels, then walking from pure noise down to clean images.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Sampling
  • Generative models
  • ReadReady to readNot yet

TL;DR

Song and Ermon's 2019 paper makes new images without a GAN and without a model that must be normalised. It corrupts training images with Gaussian noise at ten strengths, trains one network to estimate the score (the direction toward more likely data) at every strength using Score Matching, and then samples with annealed Langevin dynamics: start from noise, walk at the strongest noise level, then step down to weaker levels. On CIFAR-10 the unconditional model reached an inception score of 8.87, which the paper reports as a new state of the art for that setting.

Builds on: Score Matching, Langevin Dynamics, Annealed Langevin Dynamics · Leads to: the 2020 SDE follow-up, Diffusion Model

Why it mattered

For most of 2015 to 2019, high-quality image generation meant either a GAN, which trains a generator against a discriminator and can be unstable, or a likelihood model such as a Variational Autoencoder, autoregressive model or Normalizing Flow, which needs a special architecture or a surrogate loss. This paper showed that a plain network with no adversary and no normalisation constraint could produce samples that look comparable to GAN samples on MNIST, CelebA and CIFAR-10.

Its idea of learning a denoising direction at many noise levels fed directly into the diffusion family. Work from 2020 such as Denoising Diffusion Probabilistic Models (DDPM) trained noise predictors in a closely related way, and the 2020 SDE paper of Song and colleagues wrote the noise levels as a continuous process, placing score-based and diffusion models on one footing. Today's image generators built on diffusion models inherit the vocabulary of this paper: a score, a noise schedule, and a sampler that walks down that schedule.

History

Aapo Hyvärinen introduced score matching in 2005, as a way to fit an unnormalised density without computing its normalising constant. Pascal Vincent showed in 2011 that a denoising version connects the score to a denoising autoencoder. Simulated annealing, a search method that slowly lowers a temperature, dates to the 1980s and inspired the annealing schedule.

Before 2019, score-based sampling with Langevin dynamics was known but did not produce good images, for the three reasons in the problem section. Song and Ermon's combination of many noise levels, one conditional network and annealed sampling made it work on real image datasets. The approach caught on because it avoided the instability of adversarial training and was easy to state. The 2020 SDE follow-up placed the family on a continuous-time footing, and diffusion models then took it up widely.

The problem

Before this paper, the natural route to sampling from a density p(x)p(\mathbf{x}) was to learn its score ∇xlog⁡p(x)\nabla_{\mathbf{x}} \log p(\mathbf{x}) with score matching and then run Langevin dynamics. The authors identified three reasons this fails on images.

  1. Manifold hypothesis. Natural images occupy a thin, low-dimensional surface inside the huge space of all pixel grids. Off that surface the density is essentially zero, so the score is either undefined or meaningless. Score matching has nothing sensible to fit there.
  2. Low-density regions. Where the training data is sparse, score matching sees few examples. Its estimate of the score is poor exactly in the places a sampler wanders through when it starts from noise.
  3. Slow mixing. If the density has two separated peaks, a Langevin walk started on one peak tends to stay there. The relative weights of the peaks come out wrong. The paper's toy experiment uses a mixture of two well-separated Gaussians (a Mixture Model) and shows standard Langevin dynamics getting the weights wrong.

The fix is to add noise on purpose. Noise fills in the empty regions and connects the peaks, and it makes the score defined everywhere in pixel space.

How it works

The pipeline runs in four stages: choose noise levels, train one network on all of them, sample by walking down the levels, and output an image.

flowchart LR
  A[Clean training images] --> B[Add noise at 10 levels]
  B --> C[Train one score network]
  C --> D[Start from uniform noise]
  D --> E[Langevin walk at the largest noise]
  E --> F[Langevin walk at each smaller noise]
  F --> G[Walk at the smallest noise]
  G --> H[New image]

We follow one tiny case in one dimension, so that every step is a number you can check. The clean data point is x=2.0x = 2.0.

  1. Choose the noise levels. The paper uses ten levels from σ1=1\sigma_1 = 1 down to σ10=0.01\sigma_{10} = 0.01, each about 0.6 times the one before. Check: 1×0.69≈0.0101 \times 0.6^9 \approx 0.010.
  2. Corrupt the clean point. Pick σ=0.5\sigma = 0.5 and a standard normal draw η=−1.0\eta = -1.0 (a number from a random generator). The noisy point is x~=x+ση=2.0−0.5=1.5\tilde{x} = x + \sigma\eta = 2.0 - 0.5 = 1.5. Check: the noisy point sits below the clean one, as the draw was negative.
  3. Compute the target direction. The best direction to move from x~\tilde{x} back toward xx is −(x~−x)/σ2=−(1.5−2.0)/0.25=2.0-(\tilde{x} - x)/\sigma^2 = -(1.5 - 2.0)/0.25 = 2.0. Check: the target is positive, so it points up, toward the clean value.
  4. Train the network to output that direction. Let the network output sθ(1.5,0.5)=1.6s_\theta(1.5, 0.5) = 1.6. The per-point loss is 12(s+(x~−x)/σ2)2=12(1.6−2.0)2=0.08\tfrac{1}{2}\big(s + (\tilde{x} - x)/\sigma^2\big)^2 = \tfrac{1}{2}(1.6 - 2.0)^2 = 0.08. Check: if the output were exactly 2.0, the loss would be zero.
  5. Take one sampling step. Start at x0=0.0x_0 = 0.0 with step size α=0.02\alpha = 0.02. Let the network output s=2.0s = 2.0 and draw noise z=0.5z = 0.5. The update x←x+α2s+α zx \leftarrow x + \tfrac{\alpha}{2} s + \sqrt{\alpha}\, z gives 0.0+0.01×2.0+0.1414×0.5=0.02+0.0707=0.0910.0 + 0.01 \times 2.0 + 0.1414 \times 0.5 = 0.02 + 0.0707 = 0.091. Check: the drift term pushes the point up by 0.02, and the noise term adds a random kick of 0.07. Here the kick is larger than the drift, so single steps look noisy.
  6. Anneal. Repeat step 5 at each noise level, from largest to smallest, shrinking the step size as the noise shrinks. The paper sets αi=ε σi2/σL2\alpha_i = \varepsilon\,\sigma_i^2/\sigma_L^2, where ε\varepsilon is a small constant (the paper uses 2×10−52 \times 10^{-5}) and σL\sigma_L is the smallest noise level, 0.010.01. Check: at σ1=1\sigma_1 = 1 the step is 0.20.2, and at σ10=0.01\sigma_{10} = 0.01 it is 2×10−52 \times 10^{-5}.

The figure below shows the score for two noise levels around a data point at zero. The steeper curve belongs to the smaller noise. Near the data, the score pulls harder when the noise is small.

−1−0.500.51−4−2024xscore
  • σ = 1
  • σ = 0.5
The score of a Gaussian centred at 0 is minus x over σ squared. Halving σ quadruples the slope.

The math

Let pdatap_{\text{data}} be the true image distribution. Adding Gaussian noise of scale σ\sigma gives the perturbed distribution

qσ(x)=∫pdata(t) N(x∣t,σ2I) dt.q_\sigma(\mathbf{x}) = \int p_{\text{data}}(\mathbf{t})\, \mathcal{N}(\mathbf{x} \mid \mathbf{t}, \sigma^2 I)\, d\mathbf{t}.

The network sθ(x,σ)\mathbf{s}_\theta(\mathbf{x}, \sigma) is trained to match ∇xlog⁡qσ(x)\nabla_{\mathbf{x}} \log q_\sigma(\mathbf{x}) for every σ\sigma in the list.

Step 3 in symbols

The noise kernel is N(x~∣x,σ2I)\mathcal{N}(\tilde{\mathbf{x}} \mid \mathbf{x}, \sigma^2 I). Its log is −∥x~−x∥2/(2σ2)-\|\tilde{\mathbf{x}} - \mathbf{x}\|^2/(2\sigma^2) plus a constant, so its gradient with respect to x~\tilde{\mathbf{x}} is

∇x~log⁡N(x~∣x,σ2I)=−x~−xσ2.\nabla_{\tilde{\mathbf{x}}} \log \mathcal{N}(\tilde{\mathbf{x}} \mid \mathbf{x}, \sigma^2 I) = -\frac{\tilde{\mathbf{x}} - \mathbf{x}}{\sigma^2}.

This is the target in step 3 of the walkthrough.

Step 4 in symbols

The denoising score matching loss for one level is

ℓ(θ;σ)=12 Epdata(x) Ex~∼N(x,σ2I)[∥sθ(x~,σ)+x~−xσ2∥22].\ell(\theta; \sigma) = \tfrac{1}{2}\, \mathbb{E}_{p_{\text{data}}(\mathbf{x})}\, \mathbb{E}_{\tilde{\mathbf{x}} \sim \mathcal{N}(\mathbf{x}, \sigma^2 I)} \left[ \left\| \mathbf{s}_\theta(\tilde{\mathbf{x}}, \sigma) + \frac{\tilde{\mathbf{x}} - \mathbf{x}}{\sigma^2} \right\|_2^2 \right].

The levels are combined into one objective with a weight λ(σi)\lambda(\sigma_i):

L(θ)=1L∑i=1Lλ(σi) ℓ(θ;σi).\mathcal{L}(\theta) = \frac{1}{L} \sum_{i=1}^{L} \lambda(\sigma_i)\, \ell(\theta; \sigma_i).

The paper chooses λ(σ)=σ2\lambda(\sigma) = \sigma^2. The reason is that the trained network has roughly ∥sθ∥∝1/σ\|\mathbf{s}_\theta\| \propto 1/\sigma, so without the weight the small-noise terms would dominate the loss, because the score they must fit is about 1/σ1/\sigma times larger there. With the weight,

σ2ℓ(θ;σ)=12 E[∥σ sθ(x~,σ)+x~−xσ∥22].\sigma^2 \ell(\theta; \sigma) = \tfrac{1}{2}\, \mathbb{E}\left[ \left\| \sigma\, \mathbf{s}_\theta(\tilde{\mathbf{x}}, \sigma) + \frac{\tilde{\mathbf{x}} - \mathbf{x}}{\sigma} \right\|_2^2 \right].

Here (x~−x)/σ(\tilde{\mathbf{x}} - \mathbf{x})/\sigma is a standard normal vector whose size does not depend on σ\sigma, and neither does σ sθ\sigma\,\mathbf{s}_\theta. Each level therefore contributes a loss of the same order.

Step 6 in symbols

The annealed Langevin update at level ii is

x~t=x~t−1+αi2 sθ(x~t−1,σi)+αi zt,zt∼N(0,I),\tilde{\mathbf{x}}_t = \tilde{\mathbf{x}}_{t-1} + \frac{\alpha_i}{2}\, \mathbf{s}_\theta(\tilde{\mathbf{x}}_{t-1}, \sigma_i) + \sqrt{\alpha_i}\, \mathbf{z}_t, \qquad \mathbf{z}_t \sim \mathcal{N}(0, I),

with step size αi=ε σi2/σL2\alpha_i = \varepsilon\, \sigma_i^2 / \sigma_L^2, where ε\varepsilon is the constant step scale and σL\sigma_L is the smallest noise level. Since the score is about 1/σ1/\sigma, the drift term αisθ/2\alpha_i \mathbf{s}_\theta/2 scales like σ2⋅(1/σ)=σ\sigma^2 \cdot (1/\sigma) = \sigma, and the noise term αi\sqrt{\alpha_i} also scales like σ\sigma. The ratio of drift to noise is therefore the same at every level, which is why one step-size rule works across the whole schedule.

Worked examples

Example
1. One denoising target (tiny)

Clean value x=1.0x = 1.0, noise level σ=0.5\sigma = 0.5, noisy value x~=0.8\tilde{x} = 0.8.

  1. Gap between noisy and clean: x~−x=0.8−1.0=−0.2\tilde{x} - x = 0.8 - 1.0 = -0.2.
  2. Divide by σ2=0.25\sigma^2 = 0.25: −0.2/0.25=−0.8-0.2 / 0.25 = -0.8.
  3. Negate to get the target: −(−0.8)=0.8-(-0.8) = 0.8.
  4. Interpretation: the ideal score at 0.80.8 is +0.8+0.8, which points up toward the clean value 1.01.0.
Example
2. One annealed Langevin step (standard)

Current point x~=0.0\tilde{x} = 0.0, noise level σ=0.5\sigma = 0.5, step size α=0.05\alpha = 0.05, network output s=1.6s = 1.6, noise draw z=0.5z = 0.5. Check first that α=0.2σ2=0.2×0.25=0.05\alpha = 0.2 \sigma^2 = 0.2 \times 0.25 = 0.05, which matches the rule αi=0.2 σi2\alpha_i = 0.2\,\sigma_i^2 for this schedule.

  1. Drift term: α2 s=0.025×1.6=0.04\tfrac{\alpha}{2}\, s = 0.025 \times 1.6 = 0.04.
  2. Noise scale: α=0.05=0.2236\sqrt{\alpha} = \sqrt{0.05} = 0.2236.
  3. Noise term: 0.2236×0.5=0.11180.2236 \times 0.5 = 0.1118.
  4. New point: 0.0+0.04+0.1118=0.15180.0 + 0.04 + 0.1118 = 0.1518, about 0.1520.152.
  5. Interpretation: the drift moves the point up by 0.040.04 toward higher density, and the random kick adds 0.110.11. Here the kick is larger than the drift, which is why many short steps are needed.
Example
3. The paper's schedule on a CIFAR-10 image (realistic)

CIFAR-10 images are 32×3232 \times 32 pixels with three colour channels, so one image has 30723072 numbers, and the score network outputs 30723072 numbers at each query. The paper samples with L=10L = 10 levels and T=100T = 100 steps per level.

  1. Count the network calls per image: 10×100=100010 \times 100 = 1000.
  2. Step size at the largest noise: σ1=1\sigma_1 = 1 gives α1=0.2×12=0.2\alpha_1 = 0.2 \times 1^2 = 0.2.
  3. Noise level at index 5: the ratio between levels is 0.011/9≈0.59950.01^{1/9} \approx 0.5995, so σ5=0.59954≈0.129\sigma_5 = 0.5995^4 \approx 0.129.
  4. Step size there: α5=0.2×0.1292=0.2×0.0167=0.0033\alpha_5 = 0.2 \times 0.129^2 = 0.2 \times 0.0167 = 0.0033.
  5. Step size at the smallest noise: α10=0.2×0.012=2×10−5\alpha_{10} = 0.2 \times 0.01^2 = 2 \times 10^{-5}.
  6. Interpretation: the late steps are about ten thousand times smaller than the first, so the final walk makes only fine-grained changes to the image.

Try it: with the same ε=2×10−5\varepsilon = 2 \times 10^{-5} and σL=0.01\sigma_L = 0.01, what is the step size α\alpha at σ=0.1\sigma = 0.1?

Answer: α=ε σ2/σL2=2×10−5×0.01/0.0001=2×10−5×100=0.002\alpha = \varepsilon\, \sigma^2 / \sigma_L^2 = 2 \times 10^{-5} \times 0.01 / 0.0001 = 2 \times 10^{-5} \times 100 = 0.002.

Results

The paper evaluates on three datasets: MNIST, CelebA cropped to 140×140140 \times 140 and resized to 32×3232 \times 32, and CIFAR-10. All pixel values are scaled to the range from 0 to 1. The model uses L=10L = 10 levels with σ1=1\sigma_1 = 1 and σ10=0.01\sigma_{10} = 0.01, and sampling uses T=100T = 100 and ε=2×10−5\varepsilon = 2 \times 10^{-5}.

ResultValueWhat it means
CIFAR-10 inception score (unconditional)8.87Higher is better; the paper reports this as state of the art for an unconditional model
CIFAR-10 FID25.32Lower is better; comparable to strong GAN models such as SNGAN
Samples on MNIST, CelebA, CIFAR-10QualitativeQuality described as higher than or comparable to likelihood models and GANs
Inpainting on CelebA and CIFAR-10QualitativeThe same sampler fills occluded regions, including arbitrary shapes

The limits are as important as the numbers. The paper does not report scores for MNIST or CelebA, because those numbers are not widely reported and preprocessing differs. The FID and inception numbers depend on the evaluation setup and are not a full measure of sample diversity. The sampler is slow. The authors report that results are robust to the number of steps TT and to ε\varepsilon between 5×10−65 \times 10^{-6} and 5×10−55 \times 10^{-5}; the noise range from 1 down to 0.01 is their fixed choice. The inpainting results are shown as examples, not measured.

Common mistakes and tradeoffs

It is tempting to think the network outputs an image. In fact it outputs a direction, one number per pixel, that says which way to move. Images come from repeatedly applying that direction with noise.

It is tempting to think a single small amount of noise is enough. In fact the paper's baseline that uses only σ=0.01\sigma = 0.01 fails to produce reasonable images, because at that scale the score carries no information about low-density regions and the walk mixes poorly. The large levels are what make the sampler work.

It is tempting to read "annealed" as "lower learning rate during training". In fact annealing happens during sampling: the noise level falls and the step size shrinks, while the network is trained once on all levels.

It is tempting to treat the training loss as a likelihood. In fact it is a score-error measure. The paper notes that with λ\lambda and the levels fixed it can compare two networks, but it is not a probability of the data.

When to use this approach: when you want a generator without adversarial training or a normalised model, and when you can afford many network calls per sample. When not: when sampling must be fast, since the paper's sampler calls the network 1000 times per image. Alternatives include GANs, which need a single network pass, and later diffusion samplers, which reduce the number of steps.

Reading guide

Read the paper in this order, with the lessons that cover what each section needs.

Check yourself

QuestionWhat does the trained network s_θ(x, σ) estimate?Show answer

It estimates the score of the σ-perturbed data distribution, the gradient of the log of q_σ with respect to x. It is one network for all noise levels, given σ as input, and it outputs a vector the same size as the image.

QuestionA clean value is x = 3.0 with noise level σ = 0.2, and the noisy value is x̃ = 3.1. What is the denoising target −(x̃ − x)/σ²?Show answer

The gap is 3.1 − 3.0 = 0.1. The variance is σ² = 0.04. So the target is −0.1 / 0.04 = −2.5. The negative sign means the point should move down toward the clean value 3.0.

QuestionWhy does annealed sampling start at the largest noise level instead of the smallest?Show answer

At large noise the perturbed density has no isolated modes and its low-density gaps are filled, so the score is well defined and Langevin walks mix quickly. The samples from that level are then good starting points for the next, smaller level. Starting at the smallest level would begin the walk in a poor region with no way out.

QuestionA team trains the network with only σ = 0.01 on 64 by 64 images and gets noisy, incoherent samples. What is the likely cause, and what is the fix?Show answer

One small noise level gives no score information in low-density regions, and the walk mixes poorly, which is the baseline failure in the paper. The fix is to train one conditional network on a geometric range of levels, from a large value such as 1 down to a small value such as 0.01, and then anneal the sampler through those levels from largest to smallest.

Sources

Read the full paper

Yang Song, Stefano Ermon, 2019. arXiv:1907.05600

NCSN paper explained · ungoon.me