Diffusion via Score Matching
Continue with Diffusion, I read part 3 of chapter 20 of the book "Deep Learning" by Bishop Father and Son. Here's my note along the way. Similar to previous post on ddpm, there could be a lot of mistakes.
Note
Instead of DDPM, we look at diffusion from the score matching perspective.
The score function:
(Note that this is the gradient w.r.t. .) For example, below is visualization of score function of a mixture of Gaussians 
Why is it useful? Because if we know the score function, we can generate new data using Langevin sampling:
Another cool characteristic of the score function is that it doesn't depend on the normalization constant. I.e., if has the same score function as :
then:
More precisely, is constant within each connected component of the support. If and are both normalized and have connected common support, then .
So to learn , we just need a model .
If we learn , we can use the score without having to learn the normalization constant of .
To match the two:
We don't know , so we use the empirical distribution:
with = Dirac delta function,
Informally, it is an infinitely narrow spike at ; formally, it is a distribution rather than an ordinary function.
Thus, is not differentiable either. We therefore work with a "smeared out" / smoothed density:
Often used:
As , approaches a Dirac delta centered at , so approaches in the distributional sense.
To sum up, we wanna learn , but we only have samples from it. We smooth the distribution into and approximate expectations over using the empirical distribution . As , approaches , and the noisy sample stays closer to .
So instead of matching directly, we match the score of the smooth density . With a small and a lot of data, can approximate the underlying data distribution while still having a well-defined score.
Now:
Since:
we'll show that:
with:
We'll expand the and compare the corresponding pairs of terms.
- First terms:
and
For the second:
(by definition)
So they're the same.
- Second terms:
and
Both are independent of considered constants.
- Cross terms:
and
We have the first cross term:
Second:
We can move through the integral over
So the cross terms are equal.
Therefore:
So we can just use (use notation from now):
And by replacing with :
Since:
And because:
then:
So we can write:
So the score model predicts scaled negative noise. Equivalently, if a model predicts the noise directly, as in DDPM, its score estimate is
The book mentions 3 problems with this approach:
1. “If the data distribution lies on a manifold of lower dimensionality than the data space, the probability density will be zero at points off the manifold and here the score function is undefined since is undefined.”
2. “In regions of low data density, the estimate of the score function may be inaccurate since the loss function (20.43) is weighted by the density. An inaccurate score function can lead to poor trajectories when using Langevin sampling.”
3. “Even with an accurate model of the score function, the Langevin procedure may not sample correctly if the data distribution comprises a mixture of disjoint distributions.”
All three can be mitigated by using a large enough noise level . We discussed point 1 and 3 in this post about langevin sampling on mixtured of disjoint distributions.
But a large smears the data too much.
instead, learn at multiple noise levels:
The score network then also takes in the noise level, and the loss is weighted:
This is closely related to DDPM. After reparameterizing the score model as a noise-prediction model, both train by predicting the noise added at different noise levels. Their exact weighting and noise schedules can differ.
The book mentions that at inference time, we go from the highest noise level to the lowest. At each level, we run a few steps of Langevin sampling.
While DDPM is very clear, as we keep adding noise from one step to the next, score-matching inference is a bit less intuitive, in my opinion. The way I think about it is like dropping paint onto the surface of water and watching it slowly spread out, or "smear out." If we take snapshots at different times, they are like the different noise levels used in score matching. Although each noisy distribution is constructed directly from the original data distribution during training, level can still be viewed as level smeared out a bit more.
During inference, we first use multiple Langevin steps to move toward the distribution at level . Once we have a good sample from that distribution, it provides a useful starting point for level , especially when the two noise levels are close. Repeating this process gradually moves the sample from a heavily smeared-out distribution toward the data distribution.
| DDPM | NCSN / Score Matching |
|---|---|
| Noise schedule | Noise schedule |
| Predict | Predict |
| Reverse diffusion update | Langevin update |
| One reverse step per | Multiple Langevin steps per |
Training & Inference
The book doesn't include the training algorithm, but from the objective above it should look like this:
For each training step:
Sample a minibatch from the training data.
For every , sample a noise-level index:
- Sample Gaussian noise and perturb each data point directly:
- Use the score network to predict:
- Compute the weighted denoising score-matching loss:
- Take an optimizer step using .
A common choice is , which prevents the small-noise levels, with targets proportional to , from dominating the loss.
Of course, while the network takes in , it doesn't take in that value literally, but rather an embedding representation of it. This is similar to how in DDPM, the network takes in the time step . We use the same implementation to represent in network as well, i.e. sinusoidal embedding.
At inference time, we go from highest noise level to smallest. At each level, we run a few Langevin sampling steps using:
What's a good starting point for inference? At the highest noise level,
If the data is centered and is very large compared with the scale of , the noise term dominates, so we can initialize with
Example
I gave this note to Codex and asked it to write a code sample similar to that of DDPM. I trained for 100 epochs, just like with DDPM. The result surprisingly doesn't look very good. There could be many reasons, the first one I can think of is that Langevin sampling probably needs more tuning with that step size. However, overall the result still looks like the model has learned the general structure of MNIST. The book does mention that we can reframe this problem as SDE and reverse SDE, which opens the door to using more advanced SDE solver and get better results. Let's leave that for another day.
References
- Bishop, C. M., & Bishop, H. (2023). Deep Learning: Foundations and Concepts. Springer Nature.
- Song, Y., Sohl-Dickstein, J., Kingma, D. P., Kumar, A., Ermon, S., & Poole, B. (2021). Score-Based Generative Modeling through Stochastic Differential Equations. arXiv:2011.13456. https://arxiv.org/abs/2011.13456