The missing vector field
The Fokker-Planck section showed that the Ornstein-Uhlenbeck process moves its density with the probability flow velocity Generation runs the process backward. In the reverse clock , with , time increases while you generate, and the reverse SDE and the reverse probability flow ODE are The drift comes with the forward process. The score is the only unknown in all three equations. This section asks how you can learn the score from samples of the data, on an example where the exact score is known and every estimate can be checked against it. The data distribution is the Gaussian mixture of the probability transport chapter, with weights , means and standard deviations . At each fixed , has the distribution of with independent of . The horizon is , as in the Fokker-Planck section. The mixture has a smooth density, so its score stays finite as , but two related quantities do not stay finite. The denoising targets used later on this page have variance , which grows without bound. The samples themselves form an empirical distribution with no density at , so its score does not exist there. Training and evaluation therefore use with .Code
Code
Four fields
Four fields appear in these equations, and they are easy to confuse. The forward drift is the mean motion of one particle of the forward SDE. The score points in the direction in which the log density increases. Particles that move with the probability flow velocity have density at every . Particles that move with the reverse velocity in the reverse clock retrace the same densities from back to . The figure shows all four at , , and , under the density at the same time.The Gaussian equilibrium
The limit makes the difference between the score and the velocity exact. For the score is , which is nonzero away from the origin, and Particles of the forward SDE keep moving, and the density stays put. The score is a property of the density at one time. It is neither the velocity of a particle nor the field that transports the density. The check evaluates both fields on .Code
Code
Code
Code
The score as a posterior average
Each component of is Gaussian, with mean and variance , so its score is the straight line . Differentiate and each component score picks up a weight: The weight of component is the posterior probability that the point came from component . These are the responsibilities of the EM algorithm, evaluated on the noisy mixture . Let be the component label. Then the formula says The score of the marginal density is the posterior average of the conditional scores. The same conditional expectation comes back in the two denoising sections below, with the clean data point as the hidden variable.Code
Code
Code
Code
Learning a score without the density
Suppose you have samples of and no formula for it. You cannot evaluate , but you can still fit a model to it. Start from the squared error weighted by the data density, Expand the square. The term in does not depend on . The cross term is , and one integration by parts turns it into , provided vanishes at infinity. Up to a constant, This is the score matching objective of Hyvärinen (Hyvärinen, 2005). It needs only samples of and the derivative of the model with respect to its input.Code
Code
Code
Code
Learning from noisy observations
Fix one noise level . Given the clean point , the noisy point is Gaussian with mean and variance . Its conditional score is known in closed form: Each data point and each noise draw give a regression pair , and forming the pair needs no density. The least squares minimizer over all functions of is the conditional mean of the target: The last step is the identity of the previous section, with the continuous hidden variable in place of the label . To see it, differentiate under the integral and divide by . Regression on noisy pairs therefore recovers the score of the noisy marginal. This is denoising score matching (Vincent, 2011).Code
Code
Code
Code
Learning a family of score fields
Sampling needs the score at every between and . One network with the time as a second input can learn the whole family. Its loss averages the single-level loss over noise levels, with drawn from the data, and drawn from a distribution on . The target has variance , which blows up as . Without a weight, the smallest noise levels would dominate the loss and its gradient. The weight puts every level on the same scale. Write the network as a noise predictor, . Then so the weighted loss is . This is the noise prediction loss of DDPM, written in continuous time (Song et al., 2020). The two parameterizations have the same minimizer, but they can train differently, because the optimizer acts on outputs of very different scale. Internally the network predicts noise. Its last layer produces a value of unit scale, which is divided by to return a score. Its time input is , mapped to , together with three sine and cosine features of it. Training times are drawn with uniform on , so the small noise levels, where the score changes fastest, get as many samples as the large ones. The two forms of the loss are first evaluated on the same batch.Code
Code
Code
Code
Code
Code
Code
Code
From density gradients to sampling
The learned score tells you in which direction the log density increases. The simplest way to use it is to follow it. Fix a noise level and move particles with The noise level selects the target and stays fixed, while the simulation time runs. Each particle climbs to the local maximum of whose basin of attraction contains its starting point, and stops there, so gradient ascent returns points at the modes and not samples of . Adding noise changes that. Langevin dynamics is an SDE with drift and unit diffusion. Its Fokker-Planck equation has zero flux when . Because , the density satisfies this, which is the equilibrium condition of the Fokker-Planck section. Langevin dynamics started from therefore stays at . Started anywhere else it converges to , at a rate set by how hard it is to cross the low-density gaps between the modes.Code
Code
Code
Code
Generating samples with the reverse SDE and the probability flow ODE
The reverse SDE and the reverse probability flow ODE from the first section anneal continuously, with no separate Langevin chain at each level. The Fokker-Planck section ran the probability flow ODE backward with the exact score and recovered . The reverse SDE passes through the same marginals in reverse order (Anderson, 1982), and both samplers work with a learned score in place of the exact one (Song et al., 2020). In the reverse clock the integrators below step from to , with steps evenly spaced in so that they shrink where the score changes fastest. The SDE uses Euler-Maruyama steps. The ODE uses Heun’s method, a second-order scheme.
Run A measures integration error. Comparing B with A shows the prior gap, the effect of starting from in place of . Comparing C with A shows the error of the learned score. Run D is the practical model, which needs nothing but the samples. The effects need not add up, so D is measured directly.
Code
Code
Code
Code
Code
Code
Further connections
Conditional generation needs the score of for a condition such as a class label. Bayes’ rule splits it into two terms, . The first is the unconditional score learned on this page. The second is the gradient of a classifier that predicts from a noisy . Adding the two steers the same samplers toward samples that match . The network on this page predicts noise internally and returns a score, and other parameterizations are equally valid. The DDPM section collects them in one table: noise, clean data, score, v-prediction and the probability flow velocity, each computable from the others for . The probability flow ODE needs only the velocity , and the score is a step on the way to it. For the same Gaussian path, regressing onto gives the score. Regressing onto , the time derivative of along one noisy path, gives the velocity directly, by the same conditional mean argument. If the velocity is what you need, why estimate the score first?Numerical checks
In one dimension the probability flow ODE has an exact solution. It preserves the order of particles, so it maps each point to the point with the same CDF value: , the monotone quantile map of the probability transport chapter. Applied to the exact starting samples of run A, it gives a reference for the ODE integrator that does not depend on it. The same map gives the prior gap of the ODE without sampling: push through it and measure to from quantile functions. Ten sets of exact samples of give the sampling floor that this gap is compared with. The last check is the cost of the small-noise cutoff: the samplers stop at , not at .Code
Code
Code
Code
Further reading
Yang Song’s lecture covers diffusion and score-based generative models (MITCBMM, n.d.). His tutorial on score-based generative modeling trains a score network on images. It uses the variance-exploding process, , in place of the Ornstein-Uhlenbeck process of this page, so it has no drift and its noise variance keeps growing with .References
- Anderson, B. (1982). Reverse-time diffusion equation models. Stoch. Process. Their Appl. DOI
- Hyvärinen, A. (2005). Estimation of non-normalized statistical models by score matching. Journal of Machine Learning Research. Link
- Song, Y., Sohl-Dickstein, J., Kingma, D., Kumar, A., Ermon, S., et al. (2020). Score-based generative modeling through stochastic differential equations. arXiv [cs.LG]. DOI
- Song, Y., Ermon, S. (2019). Generative modeling by estimating gradients of the data distribution. arXiv [cs.LG]. DOI
- Vincent, P. (2011). A connection between score matching and denoising autoencoders. Neural Comput. DOI
- MITCBMM (n.d.). Diffusion and score-based generative models. Link
Found an error on this page? Report an issue.

