ai papers
An Overview of Denoising Diffusion Probabilistic Models
Junyoung Park · 2022-11-28 · 15 min
Score matching network
Diffusion models are said to have emerged from score-matching networks. So what exactly is a score-matching network?
The figure above gives a simple introduction to score-matching networks. Let us define the gradient of a PDF (probability density function) as its score. This definition presupposes ideas from energy-based models and normalized probabilities, which I will explain in more detail below. For now, recall that a gradient is defined at a point on a function and indicates the direction in which the function value increases most rapidly.
That is why gradient-based learning, commonly used in deep learning, computes the derivative (gradient) at a particular point of the loss function and optimizes the loss by moving in the opposite direction—the direction of steepest decrease. This method is called gradient descent.
Viewed instead from the perspective of sampling from a PDF, the predicted score at a point—the gradient of the PDF—points toward a “more plausible sample.” The central idea of score matching is that repeatedly generating a sample in this direction will eventually produce a plausible one.
The example above uses MNIST handwritten digits. You can think of the process as starting from noise and gradually following the gradient uphill until a clean sample appears.
I will explain where this idea of score matching came from and, ultimately, how its history led to DDPM.
Score matching to NCSN
In energy-based machine learning, energy is defined as an unnormalized negative log-likelihood. Conversely, a normalized Gibbs–Boltzmann distribution can be treated as a probability distribution.
In other words, because the that we truly want is difficult to determine, we approach it as a normalized probability over an accessible function : . Here, is the integral of over every .
We adopted this formulation because the probability is hard to obtain directly, yet computing remains difficult.
The same kind of problem appears in neural networks. We want to optimize a network over a vast number of parameter weights, but cannot express the “function” represented by that network analytically. We therefore optimize it with gradients, defining the network as a differentiable function and training it on enough data.
What ultimately matters is not , integrated over all , but the direction in which should be optimized. If we take the negative logarithm of the expression above and compute its gradient with respect to ,
the intractable term disappears from the gradient of the probability's log-likelihood. More precisely, the score in score matching is the derivative of a function defined through a probability distribution's log-likelihood, and estimating it becomes the objective of generation. A method for score estimation was introduced in 2005; I have included the paper below for anyone interested.
In brief, the paper proves the analytically calculated result. After the derivation, the terms involving the parameter become
as shown above. The problem is that the cost of the gradient depends on the dimensionality of , making computation prohibitively slow for high-dimensional modalities such as images.
Consequently, score matching attracted less attention than methods that were easier to sample from, such as VAEs, flow-based models, and GANs. It nearly faded into obscurity in deep learning, where training speed is critical.
NCSN (Noise-Conditioned Score Network) then appeared to address this problem by adopting a denoising-autoencoder approach. It is also described as a score-matching network based on annealed Langevin dynamics.
That paper is also packed with equations, so I will give only a brief overview. Because computing the score's Jacobian and then its trace was too expensive for large-scale data, the authors reformulated the original expression.
They add a noise distribution—specifically Gaussian noise—to the original data and recover the original distribution through denoising. For this approximation to hold, of course, the amount of noise added at each step must be very small.
Assuming that a very small noise distribution has been added,
Sliced score matching was also proposed, but I will not cover it separately because its projection costs four times as much as the denoising method.
In short, NCSN with a denoising autoencoder approximates the original score-matching objective by adding noise from a predefined Gaussian distribution to the data and training the model to remove it.
Sampling uses a Markov process. The score predicted from the noisy data gives the direction from that point to the next sample—as explained earlier, toward a more plausible sample—and Langevin dynamics adds the diffusion term .
Langevin dynamics has the same form as an SDE solution for Brownian motion. The equation above omits a lengthy derivation; in short, it is one of the best-known discrete approximations of an SDE solution. We can regard as a normal distribution with mean 0 and variance 1, while serves as the standard deviation in a reparameterization. This dynamics, however, suffers from
- Manifold hypothesis
- Inaccurate score matching in low density region
- Slow mixing of Langevin dynamics
so in practice, annealed Langevin sampling is used instead.
The manifold problem means that, as illustrated above, data in a three-dimensional space may actually lie on a two-dimensional manifold (a particular lower-dimensional topology). Score matching is defined in the higher-dimensional ambient space, which is assumed to make proper sampling difficult. The low-density-region problem is that scores are predicted well around the dominant parts of the training distribution, where samples are concentrated, but inaccurately elsewhere. Slow mixing of Langevin dynamics means that when distinct distributions are mixed with weights at different scales, sampling must be able to distinguish them; conventional Langevin dynamics cannot do so with its small step size.
The details are rather involved, so the key idea is simply that sampling proceeds while the noise is gradually reduced.
There is much more to say, but let us move on after noting that a score estimator can be trained through this kind of denoising.
What Does Diffusion Actually Learn?
A diffusion model has the following training objective. To keep the explanation simple, it helps to look at the diagram comparing types of generative models.
A GAN trains its generator by having a discriminator determine whether an image produced by the generator is fake or comes from the real data distribution. A VAE works differently: with an encoder that maps into embedding space , it sends data to a manifold space and uses that representation to train the decoder, which acts as the generator. Because the real data distribution cannot be obtained, it instead learns conditionally on a latent-space distribution over that is easy to define. It also maximizes probability indirectly through the ELBO (Evidence Lower Bound) seen in the VAE objective. A flow-based model defines a flow function that encodes a sample into a latent representation, based on the assumption that if this function can be defined, it can be inverted to sample from the latent space.
Diffusion is not fundamentally different from these broader ideas in generative modeling. The word diffusion evokes a drop of ink dispersing in water or perfume spreading through the air. This is related to Brownian motion (a Wiener process). Diffusion models call this the “forward process”; sampling from noise in the opposite direction with a learned parameterized network is the “reverse process.”
Forward process in DDPM
The DDPM paper defines the gradual addition of noise to a sample as the forward process. One could view noise addition as something learnable through a reparameterization trick, much like training an encoder. The paper mentions this only as a possibility, however; its experiments use a fixed noise schedule in the forward process.*
*The experimental details describe constant, linear, and quadratic schedules (, with through ).
The forward process in DDPM adds noise to the next state () based on the immediately preceding state (), following a Markov process. In a Markov process, the next state depends only on the current state. As the figure shows, a tiny amount of noise is added to the original image () to produce the next state, ; this is repeated times until it becomes pure noise (). The forward-process term used in the paper can be written as . Using a predetermined variance schedule , the paper defines as the following sequence of Gaussian-noise additions.
Reverse process in DDPM
As discussed later, the noisy samples generated at each in DDPM serve as conditions for learning the reverse process. Expressed like a flow-based model, the generative model we want would be . Because we cannot obtain it directly, we instead learn . In contrast to the forward process, the reverse process removes noise. Repeatedly adding or subtracting Gaussian noise that is very small relative to a given signal does not differ greatly in terms of the learned distribution, allowing the model to imitate the process of creating samples from noise. In the paper's notation,
The reverse-process network , parameterized by , samples the next state conditioned on the latent at each time step . Sampling each state resembles Langevin dynamics in denoising score matching; for applications of Langevin dynamics, see the score-based-model literature. Put simply, the network looks at the noise and decides the next step, whose direction should make the result resemble the samples observed during diffusion-model training as closely as possible.
How to define Loss function?
From the discussion above, it is clear that our objective is to optimize . Every deep-learning model has a loss (cost) function suited to its training setup and purpose; here it can be derived relatively easily from the ELBO used by VAEs.
Because cannot be obtained directly, we assume it can be estimated through a known . We use —the likelihood of conditioned on the prior —and divide by the posterior. The path can be defined as encoding and the path as decoding. In this formulation, as the encoder handles the forward process and as the decoder handles the reverse process, allowing us to construct a KL-divergence expression. If we know the distribution of , the decoder's starting point for the otherwise hard-to-obtain reverse process, the encoder can be trained to follow that distribution. Unlike DDPM, a VAE defines both encoder and decoder with learnable parameters and .
The main objective of the variational Bayes method in a Variational Autoencoder is to obtain the intractable posterior with the help of an encoder. From the generation perspective the objective is the maximum likelihood (MLE) , though one could imagine other algorithms for this training, such as the adversarial approach used by GANs, rather than VAE training specifically. The key point here is that a VAE learns the decoder's posterior with help from the encoder.
Transferring this perspective to a denoising diffusion probabilistic model suggests a similar optimization.
Define the latent variable in the conventional ELBO as the time-dependent variable , and the image domain as .
Here, time indicates “how much” noise has been added relative to .
As noted earlier, a likelihood model cannot obtain the posterior, so in the expression cannot be handled. We therefore use for the final term and define a lower bound with the remaining terms; this is the variational bound. A score-matching-based DDPM can handle it through conditioning. Let us use a small algebraic trick to derive the actual expression.
We can change both the part that averages the leading over and the denominator of the following KL divergence. For now, ignore the final term.
This yields the expression above. Generalizing it to the final time gives
which admits a graphical-model formulation. Then, after expanding and rearranging it,
we obtain the expression above.
The crucial part of this rearrangement begins at , because from that point onward can be conditioned independently of . The predefined posterior in the expression above can therefore be replaced by the likelihood.
Rearranging this expression one final time gives the following result and completes the derivation of the loss.
The leading negative log-likelihood makes the sequence that creates noise from follow a Gaussian distribution, so that the reverse process can begin sampling from a Gaussian distribution. It can be understood as the VAE's regularization term (forward process Gaussian).
The second expression, containing , corresponds to the green middle section: it aligns the prediction at each reverse-process step with the predefined forward process. The final term reconstructs , the real image, from .
The tractable terms are summarized above. As the paper's derivation shows, the formula can be obtained simply by conditioning Gaussian distributions.
The objective is therefore to optimize the expression above, predicting at each step a Gaussian distribution in the direction of the next step. The original loss consists of KL divergences between Gaussian distributions. By minimizing the noise variance and increasing the number of time steps , the reverse process can follow the Gaussian distribution effectively. The model must predict the mean for its input, which can also be simplified as follows.
As shown above, the objective can be written in terms of . The authors report that either the full expression or the simple objective (which removes the normalization term ) can be optimized. This differs from NCSN, which normalizes every step to obtain a weighted loss; DDPM demonstrates empirically that this is unnecessary.
This is the change from the baseline, and it reportedly produces better performance.
Limitations
As an early paper, DDPM has many limitations; one is pixel-level interpolation. In generative models such as GANs, interpolating in latent space generally preserves the consistency of generated images. In DDPM, however, interpolation at a low level can reportedly produce an entirely different image. My interpretation is that because DDPM samples by removing noise little by little, its implicit model can optimize only a single step (). A low-level latent therefore has no awareness of the ultimately generated image . DDIM later claimed to solve this problem through implicit, non-Markovian sampling.
Other limitations are sampling and training speed. Training is time-consuming in any deep-learning task, but slow sampling itself is a major drawback for a generative model. DDIM later addressed this issue as well, and most diffusion samplers now use DDIM. I plan to review DDIM in a later post.