TL;DR
A noise-conditional score network is a single neural network that takes a noisy image and a noise level, and predicts the direction in which the image should move to look more like real data. Training it at many noise levels, from heavy to light, lets annealed Langevin dynamics generate new images: it starts from pure noise and refines it step by step. It is the model behind the 2019 score-based image generator of Song and Ermon.
Builds on: Score Matching, Annealed Langevin Dynamics · Leads to: Diffusion Model
Where it's used
Image generation. Song and Ermon trained the network on natural image datasets, including CIFAR-10, and sampled new images with annealed Langevin dynamics. Their paper reported samples whose quality was competitive with the generative adversarial networks of the time, without a discriminator.
Diffusion models. The denoisers inside modern diffusion image generators, often a U-Net, learn the same kind of object: a function of a noisy input and its noise level that points toward cleaner data. Later work by Song and colleagues, which wrote the noise process as a continuous-time stochastic differential equation, placed this family on a single mathematical footing.
Image restoration. Once a network predicts the score of an image distribution, the score can be combined with a measurement model to fill in missing pixels or sharpen blurry ones. This is one route to Super-Resolution and inpainting that does not need a separate model for each task.
How it works
We follow one toy case in one dimension, because every step is then a number you can check. The data is a single value drawn near , and the network must output a score , which is the slope of the log-density, for a noisy value at noise level .
- Choose a noise level. During training, pick one level at random from a list, say . Check: the levels run from heavy noise to light noise, and each is 0.6 times the one before.
- Corrupt a clean example. Take a clean value and a random draw from a standard normal. Set . For and , we get . Check: the noisy value is the clean value shifted by the noise.
- Compute the target direction. The clean value lies at , which is 0.2 below the noisy value. So the best denoising direction is back towards , scaled by the noise: the target is . Check: the target is negative, because the noisy value sits above the clean one.
- Ask the network. Feed the network and the level . Suppose it outputs . Check: it points the right way, but not far enough.
- Measure the miss and weight it. The squared gap is . Multiply by so that heavy and light noise levels contribute on a similar scale: the weighted loss is . Check: training pushes this number down over many examples and levels.
- Sample at test time. Start from pure noise at the heaviest level, run a fixed number of Langevin steps using , then move to the next lighter level and repeat. After the last level the value is a sample.
The plot shows why the score changes with the noise level. For a standard normal data distribution, the blurred version at noise level has a score . The arrows get shorter as the noise grows, so a heavily noised sample is pulled gently toward the centre, and a lightly noised sample is pulled sharply toward the data.
- σ = 0 (clean)
- σ = 0.3
- σ = 1
The training and sampling loops share one network, and the only extra input is the noise level:
flowchart LR A[Clean image x] --> B[Add noise at a random level sigma] B --> C[Noisy image and sigma] C --> D[Score network s of x and sigma] D --> E[Loss: weighted gap to the denoising target] F[Pure noise] --> G[Langevin steps at sigma 1] G --> H[Langevin steps at each smaller sigma] H --> I[Sample]
The math
Step 1 in symbols: the noise levels
Fix levels in decreasing order, , spaced geometrically:
For , and , the ratio between neighbours is , which gives the list in step 1.
Step 2 in symbols: corruption and the target
Each training example is with . The conditional density of the noisy value given the clean one is Gaussian, so its log-gradient with respect to the noisy value is
This is the target direction from step 3. Its size is , which is large for small and small for large . Matching this target is denoising score matching, the version of Score Matching that avoids computing the true score of the data.
Step 3 in symbols: the weighted loss
The loss sums the weighted squared error over levels, where is the Expected Value over clean examples and noise draws:
The second form uses , which is equivalent to the target in step 2. The factor makes the squared term equal to , so each level has a loss of comparable size. Without it, the lightest levels would have targets of size and dominate training.
Check with the numbers from the walkthrough: , and . Then , and . The same value comes from , which confirms the two forms agree.
Step 4 in symbols: conditioning on the level
The network receives as an input. In the 2019 paper the level enters through conditional normalisation layers, each holding a separate scale and shift for every level, so one set of weights serves all levels. You can think of it as a conditional network: the same function, with its behaviour steered by an extra number.
Step 5 in symbols: annealed sampling
Start from . For each level from to , run steps of
A step size that shrinks with the level, , keeps the walk moving less at light noise. The 2019 paper used with .
Worked examples
Take a current value , the score at noise level , and a step size . The exact score of the blurred standard normal, which the trained network approximates, is , so . Use a random draw .
- Score: . Check: it points back toward 0, as it should.
- Drift term: .
- Noise term: .
- New value: .
- Interpretation: the value moved slightly toward the centre, and the random kick pushed it up. Over many steps, the drift balances the kicks and the walk spreads over the distribution, instead of collapsing to 0.
Clean value , noise level , noise draw .
- Noisy value: .
- Target score: .
- Network output: suppose .
- Unweighted squared gap: .
- Weighted by : .
- Interpretation: the network should output something closer to . Training lowers this number over many examples, and the weight is what keeps a light-noise level from being drowned out by the heavy-noise ones.
Use levels from to , with Langevin steps per level and .
- Ratio between neighbouring levels: .
- Step size at the first level: .
- Step size at the last level: . Check: the size shrinks by a factor of , which matches .
- Network calls per sample: .
- Interpretation: each sample costs a thousand forward passes of the network. This is the main cost of the method, and it is why later diffusion samplers focused on needing fewer steps.
Try it: if is halved to 50 and stays at 10, how many network calls does each sample need? Answer: .
Common mistakes and tradeoffs
It is tempting to think that one model at one noise level is enough. In fact a single level has a score field that is either too smooth to show the fine structure or too sharp to let the walk cross between regions. The point of conditioning on the noise level is that the same network gives a useful score at every scale, and the sampler moves from one scale to the next.
It is tempting to think that the weight is an arbitrary tuning knob. In fact it is what makes the levels comparable. The unweighted target has size , so without the weight the lightest levels would dominate the gradient and the network would learn fine detail at the expense of coarse structure.
It is tempting to think that the sampler is cheap because the training is simple. In fact the sampler needs network calls per sample, which is hundreds to thousands. Quality depends on both the number of levels and the number of steps, and the two must be tuned together.
When to use it: when you want a generative model with a simple, stable training loss and a score you can reuse for conditional tasks such as inpainting. When not to: when you need samples in one or two network calls; that is the motivation for the later diffusion samplers, and for distilled or flow-based generators. Alternatives: a Generative Adversarial Network gives a single pass per sample but trains less stably; a Diffusion Model uses a similar denoising objective with a different parameterisation of the noise process.
History
Before 2019, the most successful image generators were generative adversarial networks, which were hard to train, and likelihood-based models, which needed the normalising constant of the density. Score matching, introduced by Hyvärinen in 2005, offered a way to fit a density without that constant, but its results on images were poor, and it was not clear how to sample from a learned score.
Song and Ermon, in 2019, combined two ideas. Denoising score matching, which trains on noisy copies of the data, made the score estimate reliable. Annealed Langevin dynamics, which runs the walk at decreasing noise levels, made sampling from the estimate work. A single network conditioned on the noise level served all the levels. Their paper reported image samples on standard benchmarks that were competitive with the generative adversarial networks of the time.
In 2020, Song and colleagues showed that the discrete set of noise levels could be replaced by a continuous process described by a stochastic differential equation, and that the earlier diffusion models and this score-based approach were two views of one family. That unification is why the two names now appear together in the literature.
Recap
- Score matching learns the direction toward more likely data without the normalising constant.
- Denoising score matching learns that direction from noisy copies of the data, with a target set by the noise level.
- A noise-conditional score network takes the noise level as an extra input, so one model works at every scale.
- Weighting each level's loss by keeps all levels in training on a similar scale.
- Annealed Langevin dynamics samples by walking at each level in turn, from heavy noise to light, which costs network calls per sample.
Read next: Diffusion Model for the discrete-step family this lesson leads to, and Super-Resolution for an application of learned scores to restoring images.
Check yourself
QuestionWhy does the network need the noise level as an input, rather than one network per level?Show answer
The score of the blurred distribution changes with the level: at heavy noise the field is smooth and at light noise it is sharp. One network that takes as an input can serve all levels, so the sampler reuses the same weights as it moves from one level to the next, and the model stays a single set of parameters.
QuestionWith , and clean value , what is the target score , and what is the weighted loss if the network outputs ?Show answer
The target is . The unweighted gap is . Weighted by , the loss is .
QuestionA colleague says the weight is only a matter of taste and can be dropped. Why is that a mistake?Show answer
Without the weight, the target size scales like , so the lightest levels produce the largest errors and gradients. Training would concentrate on fine detail and under-train the coarse levels, which the sampler needs first. The weight makes each level's loss comparable.
QuestionA sampler with levels and steps per level is used to make 100 images in a batch. How many network calls are made in total?Show answer
Each image needs calls, and the batch has 100 images, so the total is image-level calls. In a batched implementation these are processed in groups, but the arithmetic of the work is the same.
Sources
- Song, Ermon (2019). Generative Modeling by Estimating Gradients of the Data Distribution: https://arxiv.org/abs/1907.05600
- Song et al. (2020). Score-Based Generative Modeling through Stochastic Differential Equations: https://arxiv.org/abs/2011.13456
- Hyvärinen (2005). Estimation of Non-Normalized Statistical Models by Score Matching: https://jmlr.org/papers/v6/hyvarinen05a.html