Foundations to Frontier
Tests
H. Generative Models › H6 Energy-Based and Score-Based Models

NCSN: noise-conditional score networks

Noise-conditional score networks (NCSN; Song and Ermon, 2019) were the first score-based generators to rival GANs on images. They fix two failures of plain score matching plus Langevin sampling: scores are badly learned where data are rare, and Langevin chains cannot move mass between separated modes. The cure is many noise levels. One network s_θ(x, σ) is trained with denoising score matching at a geometric sequence of scales σ₁ > … > σ_L, and samples are drawn by annealed Langevin dynamics: a few Langevin steps at the largest σ, where the noisy density is smooth and unimodal, then at each smaller σ in turn, with step sizes α_i ∝ σ_i². This page covers the noise-conditional objective and its σ² weighting, annealed Langevin, the improved recipe of NCSNv2 (Song and Ermon, 2020) and NCSN's place as the discretised variance-exploding SDE behind modern diffusion. In the exercises you build the schedule, the loss and the sampler, then sample Song and Ermon's own toy: modes at (5, 5) and (−5, −5) with weights 0.8 and 0.2. With the exact score, plain Langevin put 0.504 of its samples in the heavy mode; annealed Langevin put 0.754. With an 8-level, closed-form score model, annealing gave 0.800 (0.71–0.81 over three seeds), while plain Langevin with the learned smallest-σ score swung between 0.65 and 1.00 and lost up to 37% of its samples.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Sharpen the picture one blur level at a time

Imagine trying to find two distant towns in fog by always walking uphill: you climb whichever hill is nearest and never learn that the other town is bigger. Now imagine first looking at a very blurred map, where the two towns are one big smudge, walking towards it, then looking at a slightly sharper map, and so on. At each step the map is simple enough to read, and by the time it is sharp you are already in the right place, in the right proportions. NCSN learns the uphill direction for every blur level with a single network and samples by walking through the blur levels from coarse to fine. In the demo, set the number of levels to 1 for plain Langevin and watch the share of samples in the big mode fall to about half.

Score lab
Annealed over 8 levels, σ = 10 → 0.1, 60 steps each. Share of samples in the (5, 5) mode: 77% (the target is 80%).
Why engineers care

NCSN’s recipe, many noise levels plus a denoising network plus a coarse-to-fine sampler, is the backbone of every diffusion model used for images, video, weather forecasting and molecular design. In the score-SDE view (Song et al., 2021) it is the variance-exploding SDE, and the same networks drive inverse-problem solvers for medical and Earth-observation imaging.

📝 Quick check (Level 0)
0/3 answered
  1. 1. NCSN trains one network for…
  2. 2. Annealed Langevin goes from…
  3. 3. Plain Langevin on separated modes tends to get wrong…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The objective, the sampler and the improved recipe

  • Two failures of single-scale score models (Song and Ermon, 2019): (1) under the manifold hypothesis the data score is undefined off the data manifold, and in low-density regions it is barely trained; (2) Langevin mixes slowly between modes separated by low-density regions, so mode weights come out wrong.
  • Noise levels: a geometric sequence σ₁ > σ₂ > … > σ_L with constant ratio γ. σ₁ large enough to cover the data (NCSNv2: about the maximum pairwise distance), σ_L small enough to be invisible.
  • Objective: ℓ(θ) = (1/L) Σ_i λ(σ_i) E|s_θ(x + σ_i z, σ_i) + z/σ_i|² with λ(σ) = σ², which makes every term of order one because |s| scales like 1/σ.
  • Annealed Langevin: for i = 1 … L, α_i = ε σ_i²/σ_L²; repeat T times x ← x + α_i s_θ(x, σ_i) + √(2α_i) z. The signal-to-noise ratio of each step is then the same at every level.
  • NCSN (2019): L = 10, σ from 1 to 0.01, T = 100, ε = 2×10⁻⁵, a RefineNet with conditional instance normalisation; CIFAR-10 inception score 8.87, FID 25.32.
  • NCSNv2 (2020): σ₁ = maximum pairwise distance; γ chosen so neighbouring noise shells overlap; s_θ(x, σ) = s_θ(x)/σ (no conditioning layers needed); ε and T chosen by a Gaussian analysis; exponential moving average of weights. CIFAR-10 used 232 levels from σ = 50 to 0.01 with T = 5, FID 10.87.
  • Final denoising: one Tweedie step x + σ_L² s_θ(x, σ_L) at the end removes the residual noise and greatly improves FID (Jolicoeur-Martineau et al., 2021).
  • Score SDE (Song et al., 2021): as L → ∞, the noise levels become the variance-exploding SDE dx = √(dσ²/dt) dW; NCSN++ reached FID 2.20 on CIFAR-10. DDPM is the variance-preserving counterpart (H7).
📝 Quick check (Level 1)
0/3 answered
  1. 1. NCSN weights the DSM loss at level σ by…
  2. 2. The annealed step size is α_i = …
  3. 3. NCSNv2 sets σ₁ to about…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Noise schedules, the NCSN loss and annealed Langevin in code

Exercise 1.

  • A geometric noise schedule.
  • The annealed step sizes α_i.
  • The σ²-weighted noise-conditional DSM loss with a random level per sample.
  • Annealed Langevin with a final Tweedie denoising step.

Practice.

  • Sample a random noise level per training example and use the σ² weighting (equivalently, predict −z from (x̃, σ)).
  • Choose σ₁ from the data (maximum pairwise distance) and enough levels that neighbouring noise shells overlap.
  • Keep an exponential moving average of the weights for sampling; NCSNv2 found it removes colour shifts and instabilities.
  • Always finish with a denoising step; report FID with and without it.
  • For speed, use the reverse-SDE or probability-flow ODE samplers of later diffusion work rather than hundreds of Langevin steps per level.
import torch
def ncsn_loss(score_net, x, sigmas):                     # sigmas: 1-D tensor, largest first
    idx = torch.randint(len(sigmas), (x.shape[0],), device=x.device)
    s = sigmas[idx].view(-1, *[1] * (x.dim() - 1)); z = torch.randn_like(x)
    return 0.5 * ((s * score_net(x + s * z, idx) + z) ** 2).flatten(1).sum(1).mean()

@torch.no_grad()
def annealed_langevin(score_net, x, sigmas, eps=2e-5, T=100):
    for i, s in enumerate(sigmas):
        a = eps * (s / sigmas[-1]) ** 2
        for _ in range(T): x = x + a * score_net(x, i) + (2 * a) ** 0.5 * torch.randn_like(x)
    return x + sigmas[-1] ** 2 * score_net(x, len(sigmas) - 1)   # final denoising
🐍 Exercise 1: schedule, loss and annealed sampler
Build a geometric noise schedule, the annealed step sizes, the σ²-weighted NCSN loss and an annealed Langevin sampler with a final denoising step; check them on N(0, I).
📝 Quick check (Level 2)
0/3 answered
  1. 1. NCSNv2 recommends sampling with…
  2. 2. The final denoising step uses…
  3. 3. Training samples one noise level…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why annealing gets the weights right. At σ₁ = 10 the noisy mixture 0.8N((5, 5), (1 + σ²)I) + 0.2N((−5, −5), (1 + σ²)I) is nearly a single broad Gaussian, so Langevin mixes in a few steps and the samples carry the global weights. As σ decreases the modes separate, but each sample is already in the basin it belongs to in the right proportion, and the small-σ steps only refine locally. The weights are decided while the landscape is still easy to cross.

Constant signal-to-noise ratio. If |σ s(x, σ)| ≈ √D (true for the optimal noise-conditional score, since σ s ≈ −E[z | x̃]), then with α = εσ²/σ_L² the drift |α s| ≈ ε σ √D/σ_L² and the noise |√(2α) z| ≈ √(2ε) σ √D/σ_L. Their ratio √ε/(√2 σ_L) does not depend on the level, so every level moves samples by the same relative amount.

Why the learned small-σ score need not be accurate everywhere. The DSM variance problem (previous pages) makes the smallest-σ head poor, especially between modes. Annealing only evaluates it after the large-σ levels have placed the samples near the data, where it is used for small local moves. Plain Langevin uses it from a uniform start, exactly where it is worst.

Exercise check. The σ²-weighted error of the learned score between the modes was 0.0027 at σ = 10 and 5.71 at σ = 0.1 (seed 0): more than a thousand times worse at the level plain Langevin relies on.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Annealing fixes mode weights because…
  2. 2. With α ∝ σ², the SNR of a step is…
  3. 3. Plain Langevin from noise relies on the score…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does annealed Langevin recover the 0.8 / 0.2 mode weights that plain Langevin misses, with exact and with learned scores?

History.

  • Y. Song and S. Ermon, “Generative modeling by estimating gradients of the data distribution” (NeurIPS 2019): NCSN and annealed Langevin, with this toy problem.
  • Y. Song and S. Ermon, “Improved techniques for training score-based generative models” (NeurIPS 2020): NCSNv2.
  • A. Jolicoeur-Martineau, R. Piché-Taillefer, R. Tachet des Combes and I. Mitliagkas, “Adversarial score matching and improved sampling for image generation” (ICLR 2021): the final denoising step.
  • Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon and B. Poole, “Score-based generative modeling through stochastic differential equations” (ICLR 2021).

Exercise 2.

  • Data: 0.8N((5, 5), I) + 0.2N((−5, −5), I), 2000 training points.
  • Noise levels: 8, geometric from σ = 10 to 0.1 (10, 5.18, 2.68, 1.39, 0.72, 0.37, 0.19, 0.1).
  • Learned score: shared features (81 RBFs of width 2 on a 9×9 grid over [−8, 8]², plus z₁, z₂, 1) of the rescaled input z = x/(1 + σ/4), with one linear head per level fit by DSM in closed form, one noise draw per point. Exact scores of the noisy mixture serve as a reference.
  • Samplers: 500 particles uniform on [−8, 8]². Annealed: 60 steps per level, α_i = εσ_i²/σ_L² with ε = 0.005 (α from 50 down to 0.005), and ε = 0.01 for comparison. Plain: 480 steps of size 0.1 with the smallest-σ score (the same number of score evaluations).
  • Metrics: share of samples with x₁ + x₂ > 0 (target 0.8), among particles that stayed within |x|∞ < 20, and the share that escaped.
measure seed 0 seed 1 seed 2
plain Langevin, exact score 0.504 0.524 0.512
annealed, exact score (ε = 0.005 / 0.01) 0.754 / 0.760 0.774 / 0.716 0.766 / 0.758
plain Langevin, learned score (escaped) 0.996 (0.000) 0.956 (0.366) 0.653 (0.044)
annealed, learned NCSN (escaped) 0.800 (0.000) 0.707 (0.010) 0.808 (0.012)
within-mode std, annealed exact / learned (true 1) 0.972 / 1.069 1.050 / 1.170 0.986 / 1.295
learned-score error at σ = 0.1 on noisy data (score size) 1.997 (2.057) 1.661 (1.971) 1.419 (1.970)
σ²-weighted error between the modes, σ = 10 / 0.1 0.0027 / 5.713 0.0022 / 4.688 0.0029 / 2.143
  • The headline reproduces: with the exact score, plain Langevin split the samples roughly by basin area (0.50–0.52), ignoring the 0.8 weight; annealing moved this to 0.75–0.77.
  • Annealing was still slightly short of 0.8 with exact scores (1.5–2.6 standard errors of 0.018 below); doubling ε did not help consistently (0.716–0.760).
  • Learned scores: the annealed NCSN gave 0.71–0.81 with at most 1.2% of particles escaping. Plain Langevin with the learned smallest-σ score was erratic (0.65–1.00) and on seed 1 lost 37% of its particles, because that head’s error was as large as the score itself (1.4–2.0 against 2.0) and far worse between the modes.
  • Within-mode spread was close to 1 with exact scores; with the learned score it was 7–30% too wide.
  • Caveat: a closed-form linear-feature model in 2-D, not a RefineNet on images; the qualitative failure of plain Langevin and the fix by annealing are exactly those reported by Song and Ermon (2019).
🐍 Exercise 2 (research): does annealed Langevin recover the 0.8 / 0.2 mode weights that plain Langevin misses, with exact and with learned scores?
Train a noise-conditional score model on Song and Ermon's 0.8 / 0.2 mixture and compare plain and annealed Langevin, with exact and learned scores, by the weight they give the heavy mode.

NCSN in AI, robotics and aerospace

  • Image and video generation: NCSN’s multi-scale denoising is the ancestor of the VE-SDE samplers (NCSN++) and, with the VP variant, of every DDPM-family model.
  • Inverse problems: noise-conditional score models are used as priors for compressed-sensing MRI, CT, deblurring and super-resolution of satellite imagery, by interleaving annealed Langevin steps with data-consistency steps.
  • Robot motion: diffusion policies and trajectory diffusers sample multimodal action sequences; annealing is what lets them commit to one of several valid grasps or paths with the right frequency.
  • Science: score-based samplers generate molecular conformations, protein backbones and weather ensembles, where getting the relative weights of modes right is the whole point.

References

  1. Y. Song and S. Ermon (2019). “Generative modeling by estimating gradients of the data distribution.” NeurIPS.
  2. Y. Song and S. Ermon (2020). “Improved techniques for training score-based generative models.” NeurIPS.
  3. A. Jolicoeur-Martineau, R. Piché-Taillefer, R. Tachet des Combes and I. Mitliagkas (2021). “Adversarial score matching and improved sampling for image generation.” ICLR.
  4. Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon and B. Poole (2021). “Score-based generative modeling through stochastic differential equations.” ICLR.
  5. P. Vincent (2011). “A connection between score matching and denoising autoencoders.” Neural Computation 23(7).