02 · Denoising Score Matching and Langevin Sampling
This topic develops score estimation and Langevin sampling independently of probability-flow ODEs. The aim is to learn a score from samples, then use it to sample a fixed density. Start with one dimension; vector formulas replace derivatives by gradients. This lecture comes before DDPM: it separates the regression problem used to learn a score from the sampling algorithm that uses it.
For a smooth positive density \(p\), its scalar score is \(s(x)=\partial_x\log p(x)\). It measures the local rate of change of log density. We first learn this quantity for a Gaussian-smoothed data distribution, then use it as the drift in a sampling procedure. No probability-flow ODE is needed here.
1. Learn from clean/noisy pairs
Draw \(X_0\sim p_{\mathrm{data}}\) and independent \(\varepsilon\sim\mathcal N(0,1)\). At a fixed noise scale \(\sigma>0\), form
\[Y=X_0+\sigma\varepsilon,\qquad p_\sigma(y)=\int\mathcal N(y;x_0,\sigma^2)p_{\mathrm{data}}(x_0)\,dx_0.\]
A denoiser trained with squared error estimates \(D_*(y,\sigma)=\mathbb E[X_0\mid Y=y]\). Tweedie’s formula connects it to the noisy marginal score:
\[s_\sigma(y)=\partial_y\log p_\sigma(y) =\frac{D_*(y,\sigma)-y}{\sigma^2}.\]
The regression averages possible clean origins conditional on the noisy input. It does not recover the particular noise realization of every pair. The score is a property of the smoothed density \(p_\sigma\), not exactly the original clean density at finite \(\sigma\).
2. Denoising score matching
The conditional Gaussian score is available for every constructed pair:
\[U=\partial_y\log p(Y\mid X_0) =-\frac{Y-X_0}{\sigma^2}=-\frac{\varepsilon}{\sigma}.\]
Minimize
\[\mathcal L_{\mathrm{DSM}}(\theta;\sigma) =\mathbb E_{X_0\sim p_{\mathrm{data}},\,\varepsilon\sim\mathcal N(0,1)} [(s_\theta(X_0+\sigma\varepsilon,\sigma)+\varepsilon/\sigma)^2].\]
At the unrestricted optimum, \(s_*(y,\sigma)=\mathbb E[U\mid Y=y]=s_\sigma(y)\). The conditional target is known even though the marginal score is not. The loss differs from marginal-score regression by a conditional-variance term independent of the predictor. This is the denoising/score-matching connection developed by Vincent (2011).
To verify the identity, differentiate the marginal density:
\[\begin{aligned} \partial_y p_\sigma(y) &=\int \frac{x_0-y}{\sigma^2}p(y\mid x_0)p_{\mathrm{data}}(x_0)\,dx_0,\\ \partial_y\log p_\sigma(y) &=\int \frac{x_0-y}{\sigma^2} \frac{p(y\mid x_0)p_{\mathrm{data}}(x_0)}{p_\sigma(y)}\,dx_0\\ &=\int \frac{x_0-y}{\sigma^2}p(x_0\mid y)\,dx_0 =\mathbb E[U\mid Y=y]. \end{aligned}\]
Although the posterior is not explicitly available during training, sampling clean/noisy pairs supplies draws from their joint law. Squared-error regression performs the conditional averaging:
\[\mathbb E[(s_\theta(Y,\sigma)-U)^2\mid Y] =(s_\theta(Y,\sigma)-\mathbb E[U\mid Y])^2 +\operatorname{Var}(U\mid Y).\]
The second term is independent of the predictor. For an empirical training distribution, the unrestricted population objective over corrupted training pairs targets its smoothed empirical score.
Equivalently, train a noise predictor with squared error and set \(s_\theta=-\varepsilon_\theta/\sigma\). Then
\[\mathbb E[(\varepsilon-\varepsilon_\theta)^2]=\sigma^2\mathcal L_{\mathrm{DSM}}.\]
This exact reparameterization will reappear in DDPM, with a different cumulative noise scale at every discrete step.
3. Once a fixed score is known: Langevin sampling
For a fully worked example with trajectories, density plots, code, and numerical error checks, see Langevin Diffusion: Sampling a Gaussian Through Dynamics.
Let \(p\) be a smooth positive target density and \(s=\partial_x\log p\). Use sampling time \(\tau\), with \(p\) held fixed:
\[dX_\tau=s(X_\tau)\,d\tau+\sqrt2\,dW_\tau.\]
Brownian increments over an interval of length \(\eta\) are independent Gaussians with variance \(\eta\). Euler–Maruyama therefore gives
\[\boxed{x_{k+1}=x_k+\eta s(x_k)+\sqrt{2\eta}\,z_k, \qquad z_k\overset{\mathrm{iid}}{\sim}\mathcal N(0,1).}\]
The score \(s(x)=\partial_x\log p(x)\) has no explicit dependence on sampling time \(\tau\) or step index \(k\). Every Euler–Maruyama step evaluates the same function at the current position \(x_k\). Its value can change because the particle moves; the target density and its score field remain fixed. Changing the numerical step size does not change the target score.
Choose an initial law \(P_{\mathrm{init}}\) and draw \(X_0\sim P_{\mathrm{init}}\) independently of the driving Brownian motion. The initial law need not be Gaussian or equal to the target: it may, for example, be uniform, Gaussian, or a point mass at a chosen position. This initialization is separate from the fresh \(z_k\sim\mathcal N(0,1)\) used at every step. Each \(z_k\) is independent of \(X_0\) and all earlier increments, and therefore of the current state \(x_k\). The initial law may itself be Gaussian, but its draw and the subsequent Gaussian increments have different roles. Initial and target supports need not coincide, but the dynamics must be defined at the starting positions. Restricted-support targets may require boundary conditions; zeros of the target density can make its score undefined. Convergence requires a separate argument, not merely a choice of initial law.
The score term is the drift; the Brownian term is the diffusion or noise term, not a Brownian drift. For a well-defined, ergodic Langevin process, suitable initializations approach the target as sampling time grows. Initialization affects how long this takes; neither an arbitrary finite running time nor a finite Euler step guarantees exact target samples. For example, the standard logistic target admits fixed starting points such as zero or six.
Score-directed drift and random spreading balance at the target distribution. For the continuous SDE, \(p\) is invariant under suitable conditions; convergence from a starting law requires additional ergodicity conditions. A finite unadjusted Euler step generally has bias, and a learned score adds approximation error. Appendix A derives the stationary density and illustrates convergence with a Gaussian target.
If the learned score is \(s_\sigma\), the intended stationary target is \(p_\sigma\). Langevin does not remove the smoothing merely by running longer. Its sampling clock is not a schedule that automatically reduces \(\sigma\).
4. First solved check: a standard Gaussian
For \(p=\mathcal N(0,1)\), \(s(x)=-x\). Deterministic score ascent \(\dot x=-x\) collapses toward zero. Langevin instead gives
\[x_{k+1}=(1-\eta)x_k+\sqrt{2\eta}\,z_k.\]
For \(0<\eta<2\), the discrete chain’s stationary variance solves
\[V=(1-\eta)^2V+2\eta, \qquad V=\frac1{1-\eta/2}.\]
It approaches the target variance 1 as \(\eta\to0\), but is not exactly 1 at a finite step. This separates finite-step sampling bias from score-estimation error.
5. Several noise levels and annealed Langevin dynamics
Train \(s_\theta(x,\sigma)\) at a decreasing sequence \(\sigma_1>\cdots>\sigma_L>0\), averaging the DSM loss over a stated distribution of noise levels with positive weights. At each level, perform several Langevin steps targeting \(p_{\sigma_i}\), then lower the noise scale. Large smoothing can make movement between modes easier; short runs at each level do not guarantee equilibrium.
This is the approach of Song and Ermon (2019). It differs from a reverse diffusion SDE designed to follow prescribed changing marginals without equilibrating at every level. The final positive noise level retains smoothing error.
6. Next: a learned reverse Markov chain
The training problem is now familiar: use known conditional Gaussian targets to estimate an unknown marginal score. Langevin supplies one sampling method, with a fixed target at each noise level. DDPM next builds a reverse Markov chain and motivates its training through the ELBO. Its simplified noise-prediction loss is another weighted denoising score objective, but its ancestral sampler follows different dynamics. After DDPM, Probability-Flow ODE develops deterministic transport and later reinterprets DDPM through a reverse SDE.
Appendix A: why score-driven Langevin dynamics preserves the target density
A.1. Target density and current particle density
Fix a smooth, strictly positive, normalized density \(p(x)\) on \(\mathbb R\). Its score is
\[s(x)=\partial_x\log p(x)=\frac{p'(x)}{p(x)}.\]
Consider the continuous-time Langevin SDE
\[\boxed{dX_\tau=s(X_\tau)\,d\tau+\sqrt2\,dW_\tau.}\]
Here \(\tau\) is sampling time. The target \(p\) remains fixed, while the density \(r_\tau\) of the moving particles can change. We assume the SDE is well posed and nonexplosive, and that the densities and boundary behavior justify the derivatives and probability balances below. On the real line, use vanishing probability flux at infinity; on a bounded interval, reflecting boundaries impose zero flux.
The question is: why should the equilibrium particle density be \(p\) when the drift is its score?
A.2. Probability flux and the Fokker–Planck equation
For a one-dimensional SDE \(dX_\tau=b(X_\tau)\,d\tau+g\,dW_\tau\) with constant \(g\), the density satisfies
\[\partial_\tau r_\tau=-\partial_x(br_\tau)+\frac{g^2}{2}\partial_{xx}r_\tau.\]
Substitute \(b=s\) and \(g=\sqrt2\):
\[\partial_\tau r_\tau=-\partial_x(r_\tau s)+\partial_{xx}r_\tau =-\partial_x J_\tau,\]
where
\[\boxed{J_\tau(x)=\underbrace{r_\tau(x)s(x)}_{\text{drift flux}} \;\underbrace{-\partial_x r_\tau(x)}_{\text{diffusive flux}}.}\]
The first flux transports particles according to the score vector field. The second describes the net spreading caused by Brownian motion: probability flows down the gradient of the current particle density.
For any fixed interval \([a,b]\), integrate the continuity equation:
\[\frac{d}{d\tau}\Pr(a\leq X_\tau\leq b) =\int_a^b\partial_\tau r_\tau(x)\,dx =J_\tau(a)-J_\tau(b).\]
The interval gains probability through its left boundary and loses probability through its right boundary, with signed flux accounting for either direction of movement.
A.3. At the target density, the two fluxes cancel
Set \(r_\tau=p\). By the definition of the score,
\[p(x)s(x)=p(x)\frac{p'(x)}{p(x)}=p'(x).\]
Hence
\[\boxed{J(x)=p(x)s(x)-p'(x)=p'(x)-p'(x)=0.}\]
It follows that
\[\left.\partial_\tau r_\tau(x)\right|_{r_\tau=p}=-\partial_x J(x)=0.\]
Thus, under the stated conditions, \(p\) is an invariant density: if \(X_0\sim p\), then \(X_\tau\sim p\) at every later sampling time. Individual particles continue moving. Their distribution stays unchanged because drift and diffusion balance at every location.
The relative coefficients matter. More generally, \(dX_\tau=c\,s(X_\tau)\,d\tau+\sqrt{2c}\,dW_\tau\), for constant \(c>0\), preserves the same target: both fluxes are multiplied by \(c\). Changing the drift coefficient without the corresponding change in noise generally changes the stationary density.
A.4. Recovering the target from the stationary equation
Conversely, suppose a smooth positive density \(r_*\) is stationary on a connected domain with zero-flux boundary conditions. Then
\[\partial_x J_*=0.\]
The flux is spatially constant, and the boundary condition makes that constant zero. Therefore
\[r_*(x)s(x)-r_*'(x)=0.\]
Divide by \(r_*(x)>0\) and substitute the score:
\[\frac{r_*'(x)}{r_*(x)}=s(x)=\frac{p'(x)}{p(x)},\]
\[\partial_x\log r_*(x)=\partial_x\log p(x).\]
Integrating over the connected domain gives \(r_*(x)=C p(x)\). Since both densities integrate to one, \(C=1\):
\[\boxed{r_*(x)=p(x).}\]
This identifies the stationary density within the stated class. It does not by itself prove convergence from every initial distribution. That requires ergodicity conditions; separated modes can also make convergence very slow.
A.5. Solved example: convergence to a Gaussian
Take \(p=\mathcal N(\mu,1)\). Its score is \(s(x)=-(x-\mu)\), so
\[dX_\tau=-(X_\tau-\mu)\,d\tau+\sqrt2\,dW_\tau.\]
Multiplying the centered process by \(e^\tau\) and integrating gives
\[X_\tau=\mu+e^{-\tau}(X_0-\mu) +\sqrt2\int_0^\tau e^{-(\tau-u)}\,dW_u.\]
The stochastic integral has mean zero and variance
\[2\int_0^\tau e^{-2(\tau-u)}\,du=1-e^{-2\tau}.\]
Assuming \(X_0\) is independent of the driving Brownian motion, at each fixed time this yields the distributional representation
\[\boxed{X_\tau\overset{d}{=}\mu+e^{-\tau}(X_0-\mu) +\sqrt{1-e^{-2\tau}}\,Z,\qquad Z\sim\mathcal N(0,1),}\]
where \(Z\) is independent of \(X_0\). As \(\tau\to\infty\), the initial-condition term disappears and \(X_\tau\) converges in distribution to \(\mathcal N(\mu,1)\). If \(X_0\) already has that distribution, the variance at every time is \(e^{-2\tau}+(1-e^{-2\tau})=1\), and the mean remains \(\mu\).
Without Brownian noise, the contraction would collapse all particles toward \(\mu\). With noise, the spreading exactly balances the contraction at unit variance.
A.6. What this guarantees for a numerical sampler
The proof concerns the exact score and the continuous SDE. Euler–Maruyama instead generates
\[x_{k+1}=x_k+\eta s(x_k)+\sqrt{2\eta}\,z_k.\]
For the Gaussian example, the centered update is an autoregression with coefficient \(1-\eta\). At stationarity its variance must satisfy
\[V=(1-\eta)^2V+2\eta \quad\Longrightarrow\quad V=\frac{1}{1-\eta/2},\qquad 0<\eta<2.\]
Thus even the exact score gives a biased stationary density at a fixed Euler step size. An estimated score introduces another source of error. Small steps and sufficient sampling time are essential qualifications in Song’s discussion of Langevin sampling.
Finally, stationarity describes each sample’s marginal law. Successive observations along one stationary trajectory are generally correlated; they are not independent draws merely because the chain is at equilibrium.
For a different use of a time-dependent score—deterministic transport through a prescribed density path—see the separate Probability-Flow ODE topic.
Continue to Reverse-SDE sampling to follow a changing density path rather than equilibrate at one noise level.