擴散模型如何透過反轉噪聲學習數據分佈
Diffusion Models: How Reversing Noise Learns a Data Distribution
擴散模型透過固定的前向過程逐步向數據加入噪聲,再訓練模型預測噪聲並逐步反向去噪,從噪聲生成樣本。訓練以噪聲預測的均方誤差為目標,文章亦説明這種方法與分數估計的關係。DDIM 可沿用同一噪聲預測器,以較少步數採樣,常見為 20 至 50 步而非 1000 步,但可能略微降低品質;文中亦整理了速度、引導及評估方面的限制。
Diffusion Models: How Reversing Noise Learns a Data Distribution
A diffusion model learns to generate data by first learning how to destroy it. Take a real image, add a little Gaussian noise, repeat that step many times, and you end up with pure noise. Nothing about that forward process needs learning. The interesting part is the reverse: starting from noise, remove a little noise at a time, and after enough steps a coherent sample appears. If each small denoising step is easy to learn, the full reverse chain can be surprisingly powerful.
That reversal is the whole trick. A single jump from noise to a realistic image is a hard, highly multimodal mapping. A hundred small jumps from slightly noisy to slightly less noisy are each close to a simple Gaussian problem. Diffusion models trade one impossible step for many tractable ones, and that trade is why they now dominate image generation and are spreading to audio, video, molecules, and planning.
The forward process: a fixed destruction schedule
Let x0 be a data point and let t=1,…,T index the noise steps. The forward process adds noise with a fixed schedule βt :
q(xt∣xt−1)=N(xt;1−βt xt−1,βtI)
The mean shrinks the previous sample slightly toward zero, and the variance adds fresh noise. Because every step is Gaussian, the distribution after t steps can be written in closed form. Define αt=1−βt and αˉt=∏s=1tαs . Then:
q(xt∣x0)=N(xt;αˉt x0,(1−αˉt)I)
Equivalently, we can sample xt directly from x0 and a standard Gaussian ϵ :
xt=αˉt x0+1−αˉt ϵ,ϵ∼N(0,I)
This closed form matters for training. We do not need to simulate the whole chain to create a training example at noise level t . We sample a data point, sample a time step, sample noise, and form xt in one line. As t grows, αˉt falls toward zero, so xT is close to pure Gaussian noise. The forward process therefore gives us a bridge from the data distribution to a distribution we can sample trivially.
The reverse process: learning small denoising steps
The reverse process is what we learn. We model it as a chain of Gaussians with learned means:
pθ(xt−1∣xt)=N(xt−1;μθ(xt,t),σt2I)
The key insight is that when βt is small, the true reverse step q(xt−1∣xt,x0) is also close to Gaussian, with a mean we can compute in closed form from x0 and xt . So the learning problem is well posed: predict a mean that approximates that conditional mean, one small step at a time.
In practice, most models do not predict μθ directly. They predict the noise ϵ that was added to make xt . Given a predicted noise ϵθ(xt,t) , we can reconstruct an estimate of x0 :
x^0=xt−1−αˉt ϵθ(xt,t)αˉt
and then form the reverse mean from x^0 and xt . The standard training objective, after simplifying the variational bound, is remarkably plain:
L=Ex0,t,ϵ[∣ϵ−ϵθ(xt,t)∣2]
That is just mean squared error on noise prediction, averaged over data points, time steps, and noise draws. There is no adversarial game, no encoder to balance, and no partition function to estimate. The complexity lives in the network, usually a U-Net with time conditioning, and in the sampling procedure, not in the loss.
A minimal training step looks like this:
import torch
def forward_noising(x0, t, alphas_bar):
noise = torch.randn_like(x0)
a = alphas_bar[t].view(-1, 1, 1, 1)
xt = torch.sqrt(a) * x0 + torch.sqrt(1 - a) * noise
return xt, noise
for x0 in dataloader:
t = torch.randint(0, T, (x0.shape[0],))
xt, noise = forward_noising(x0, t, alphas_bar)
pred = model(xt, t)
loss = ((pred - noise) ** 2).mean()
loss.backward()
optimiser.step()
The model sees noisy inputs at every noise level, from almost clean to almost pure noise, and must predict what was added. That multi-scale supervision is part of why the learned denoiser generalises across the chain.
Why noise prediction is score prediction in disguise
There is a second, deeper view. The score of a distribution is the gradient of its log-density, ∇xlogp(x) . For the noisy distribution q(xt) , the score points uphill toward regions of higher density, which is exactly the direction a denoiser should move. In fact, the optimal noise predictor and the score are linked by:
∇xtlogq(xt)=−ϵθ(xt,t)1−αˉt
up to the approximation error of the learned model. So training a diffusion model is also training a score estimator at every noise level, and sampling by repeated denoising is a form of annealed Langevin-style sampling: start in a heavily smoothed distribution where modes are easy to find, then gradually sharpen the distribution while following the score.
This connection explains a design choice that otherwise looks arbitrary. Adding noise smooths the data distribution and fills in low-density gaps, so the score is well defined almost everywhere. As noise decreases, the distribution sharpens and fine detail appears. A single score estimate at one noise level would not be enough, because the score of the clean distribution can be badly behaved in empty regions. A family of scores across noise levels is the fix.
Sampling: from noise back to data
Sampling runs the learned reverse chain. The classic DDPM sampler starts from xT∼N(0,I) and, at each step, predicts the noise, forms the reverse mean, and adds a little fresh noise (except at the final step):
xt−1=μθ(xt,t)+σtz,z∼N(0,I)
The added noise is not a flaw. It keeps the chain stochastic, so repeated runs from different starting noise give different samples, and it corrects small errors in the learned mean rather than letting them accumulate deterministically.
DDIM showed that the same trained noise predictor can drive a different sampler. By removing the per-step noise, or interpolating between stochastic and deterministic updates, DDIM can sample in far fewer steps, often 20 to 50 instead of 1000, with modest quality loss. The lesson is that the model and the sampler are separable: the network learns a denoising function, and different samplers are different ways of iterating it.
Conditioning and classifier-free guidance
Unconditional generation is rarely the goal. In text-to-image models, the noise predictor also receives a conditioning signal c , such as a text embedding, giving ϵθ(xt,t,c) . Classifier-free guidance is the standard way to strengthen that conditioning without a separate classifier.
During training, the conditioning is randomly dropped, so the same network learns both ϵθ(xt,t,c) and ϵθ(xt,t) . At sampling time, the two predictions are combined:
ϵ~θ=ϵθ(xt,t)+w(ϵθ(xt,t,c)−ϵθ(xt,t))
The guidance scale w controls the trade-off. At w=0 , sampling is unconditional. At w=1 , it follows the conditional model. Values above 1, often around 7 for image models, push samples toward the conditioning and away from the unconditional prediction, sharpening alignment with the prompt at the cost of diversity. Guidance is powerful, but it is also a common source of oversaturated, stereotyped outputs when pushed too far.
The continuous-time view: stochastic differential equations
The discrete chain has a continuous limit. The forward process becomes a stochastic differential equation:
dx=f(x,t) dt+g(t) dw
where w is Brownian motion. The reverse process is then another SDE, running backward in time, whose drift depends on the score:
dx=[f(x,t)−g(t)2∇xlogpt(x)]dt+g(t) dwˉ
This view unifies DDPM, score-based models, and DDIM: they differ in the choice of f and g , and in the numerical solver used for the reverse equation. It also opens the door to probability-flow ODEs, which share the same marginal distributions as the reverse SDE but are deterministic, allowing fast solvers and exact likelihood computation via the change-of-variables formula.
For most practitioners, the SDE view is a map rather than a daily tool. It tells you which knobs exist: the noise schedule, the sampler, the solver, and the score model. The discrete DDPM view tells you how to train that score model in the first place.
Limitations and common pitfalls
Diffusion models are strong, but they are not magic, and several failure modes recur.
First, sampling is slow relative to a single forward pass. Even with DDIM or a distilled model, generation needs many network evaluations. Distillation, consistency models, and better ODE solvers are active responses, but the speed gap with GANs and autoregressive single-pass decoders remains real.
Second, likelihoods are awkward. Diffusion models do not give a cheap exact density for a new point. The variational bound is computable, but it is a bound, and comparing bounds across model families can mislead. If you need calibrated densities or anomaly scores, a flow or an energy-based model may be a cleaner fit.
Third, guidance and sampling choices change the distribution you actually sample from. A model trained with one schedule and sampled with another, or pushed with a large guidance scale, will produce samples that look good but no longer match the learned distribution. Report the sampler, the number of steps, and the guidance scale alongside any quality claim, or the claim is not reproducible.
Fourth, data and evaluation still dominate. Diffusion models memorise training data more readily than their clean loss suggests, especially on small datasets or with many sampling steps toward a mode. Near-duplicate detection, held-out likelihood proxies, and human evaluation matter more than a single aggregate score. Metrics like FID summarise distributional similarity, but they hide mode collapse, memorisation, and prompt misalignment.
Fifth, the Gaussian reverse assumption breaks if steps are too large. The whole construction relies on small βt so that each reverse step is near-Gaussian. Aggressive step reduction without retraining or a suitable sampler degrades quality quickly. Fast sampling is a sampler design problem, not just a matter of skipping steps.
When to reach for diffusion
Choose diffusion when sample quality and diversity matter more than single-pass speed, when the data are continuous and high-dimensional, and when you can afford many sampling steps or a distilled variant. Images, audio, and molecular conformations are natural fits. Choose something else when you need exact likelihoods, very fast sampling on limited hardware, or a compact latent representation for downstream tasks: there, flows, VAEs, or autoregressive models may serve better, sometimes with a diffusion model as a prior or refiner rather than the whole system.
The enduring idea is simple enough to state in one sentence. Destroy data with a known noise process, learn to reverse each small step, and let the chain do the hard work of turning noise into structure. Everything else - noise prediction, score matching, guidance, and SDE solvers - is machinery for making that reversal accurate, controllable, and fast enough to use.
Connect with me on LinkedIn: linkedin.com/in/umeshtharukaofficial
來源:DEV Community · dev.to