Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Steering a generator with a judge
A diffusion model turns noise into a sample in many small denoising steps. Suppose you also have a judge, a classifier that says how much a half-denoised picture looks like a ‘runway’ or a ‘forest’. At every step you nudge the picture a little in the direction that makes the judge more confident. That is classifier guidance. Turn the nudge up and the samples become unmistakable examples of the class but also more alike; turn it to zero and you get the unconditional model back. The demo shows a one-dimensional mixture and what the guidance scale does to it.
Guidance is how a generator is pointed at what you want, a class, a caption or a measurement, without retraining it. The same gradient trick lets a diffusion prior be steered by a physics or sensor model, for example to reconstruct an image from a few satellite measurements.
- 1. Classifier guidance adds to the score…
- 2. A larger guidance scale gives…
- 3. The classifier used for guidance must handle…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Bayes’ rule on the score
- Conditional score: log p(x_t|y) = log p(x_t) + log p(y|x_t) − log p(y), so ∇log p(x_t|y) = ∇log p(x_t) + ∇log p(y|x_t). The last term does not depend on x and drops out of the gradient.
- Guided score (Song et al., 2021; Dhariwal and Nichol, 2021): ∇log p(x_t) + s·∇log p(y|x_t). Scale s = 1 is the exact conditional; s > 1 targets p(x_t) p(y|x_t)^s, a sharpened distribution.
- ε form: since ε̂ = −√(1−ᾱ_t)·score, the guided prediction is ε̂ − s·√(1−ᾱ_t)·∇log p_φ(y|x_t); plug it into DDIM or DDPM unchanged.
- Noise-aware classifier: p_φ(y|x_t, t) is trained on noised data at random t, usually as the encoder half of the diffusion U-Net (ADM). An off-the-shelf classifier on clean images gives meaningless gradients on noisy inputs.
- Gaussian example: prior N(0, 1) and ‘classifier’ exp(−(x−c)²/(2τ²)) raised to s give N(sc/τ² / (1 + s/τ²), 1/(1 + s/τ²)): the mean moves towards c and the variance shrinks as s grows.
- Separated modes: the mode weights of p(x) p(y|x)^s are w_k·π_k^s, normalised, where π_k is the posterior of class y in mode k; ambiguous modes (π_k ≈ 0.5) lose weight fastest.
- Successors: CLIP guidance (GLIDE, Nichol et al., 2022), classifier-free guidance (Ho and Salimans, 2022), and training-free guidance through the denoised estimate x̂₀ (DPS, Chung et al., 2023; universal guidance, Bansal et al., 2023).
- 1. Scale s = 1 with an exact classifier samples…
- 2. In ε form the guided prediction is…
- 3. Which modes lose weight first as s grows?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Guiding a sampler
Exercise 1.
- The DDPM linear schedule ᾱ_t.
- The exact score of a noised isotropic Gaussian mixture.
- The exact noisy-classifier gradient ∇log p_t(y|x).
- The guided ε.
Practice.
- Train the classifier on x_t at uniformly random t, conditioned on t (ADM uses the U-Net encoder with attention pooling).
- Typical ImageNet scales were 1 to 10; sweep s and plot precision against recall, or FID against Inception score, rather than picking one value.
- Guided gradients through a classifier look adversarial at low noise; check samples, not only classifier accuracy.
- Without a noise-aware classifier, guide through the denoised estimate x̂₀(x_t) (DPS, universal guidance) and expect to tune step sizes.
- In 2026 practice, classifier-free guidance has replaced classifier guidance for text and class conditioning; classifier-style gradients survive for inverse problems and reward or constraint guidance.
import torch
def guided_eps(unet, clf, x_t, t, y, scale, abar):
eps = unet(x_t, t)
with torch.enable_grad():
x = x_t.detach().requires_grad_(True)
logp = torch.log_softmax(clf(x, t), -1)[range(len(y)), y].sum()
grad = torch.autograd.grad(logp, x)[0] # ∇ log p_φ(y | x_t)
return eps - scale * (1 - abar[t]).sqrt().view(-1, 1, 1, 1) * grad- 1. The ADM classifier is trained on…
- 2. Choose the scale by…
- 3. Without a noise-aware classifier you can guide through…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Exactness at s = 1. With the true p(y|x_t) the guided score equals ∇log p_t(x|y) at every t, so any sampler that is correct for the unconditional model is correct for the conditional. A learned classifier p_φ only approximates p(y|x_t); its errors enter the score directly.
What s > 1 targets. The guided score is the score of p_t(x) p_t(y|x)^s at each t. These noised tilted densities are not the noised versions of one clean distribution, so the sampler does not exactly sample p(x) p(y|x)^s at t = 0; for well-separated modes it approximately reweights modes by π_k^s, with some extra concentration within each mode.
Why clean classifiers fail. Noise erases class information: the true p(y|x_t) tends to the prior as t grows, so its gradient shrinks. A classifier fitted on clean data has steep, confident decision boundaries and applies them to noisy inputs, giving gradients many times too large. Its guidance at s = 1 then acts like a much larger scale.
Adversarial gradients. Near t = 0, maximising log p_φ(y|x) by gradient can change imperceptible directions that the classifier happens to rely on, so classifier accuracy can rise without the sample improving.
- 1. With the true classifier, s = 1 gives…
- 2. As t grows, the true p(y|x_t) tends to…
- 3. A clean-data classifier on noisy inputs gives gradients that are…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How does the guidance scale trade fidelity for diversity, and what goes wrong with a classifier that never saw noise?
History.
- Y. Song et al., “Score-based generative modeling through stochastic differential equations” (2021): conditional sampling with a time-dependent classifier.
- P. Dhariwal and A. Nichol, “Diffusion models beat GANs on image synthesis” (2021): classifier guidance with a scale on ImageNet.
- J. Ho and T. Salimans, “Classifier-free diffusion guidance” (2022); H. Chung et al., “Diffusion posterior sampling” (2023).
Exercise 2. Six isotropic Gaussian modes (std 0.3, equal weights) in 2-D: class 0 on the left at (−2, ±1) and (−0.4, 0), class 1 on the right at (2, ±1) and (0.4, 0). The two central modes overlap, so they are ambiguous. We use the exact score of the noised mixture instead of a trained network, so all differences come from guidance. Deterministic DDIM, 50 steps, DDPM linear schedule, target class 1, 2000 samples per setting.
- Accuracy: share of samples whose exact clean posterior of class 1 exceeds 0.5 (a perfect conditional sampler gives about the Bayes rate, 0.971 here).
- Central share: share of samples in the ambiguous central mode; the true conditional has 1/3.
- TV: total-variation distance between sampled and true mode weights within class 1.
- Learned classifiers: logistic regression on quadratic features, either noise-aware (features also times σ_t and σ_t², trained on x_t at random t) or clean-only (trained on x₀).
| guidance | accuracy | central share | TV |
|---|---|---|---|
| exact, scale 0 (unconditional) | 0.487 | 0.163 | 0.512 |
| exact, scale 1 | 0.975 | 0.30 | 0.035 |
| exact, scale 3 | 1.0 | 0.004 | 0.329 |
| exact, scale 10 | 1.0 | 0.0 | 0.333 |
| noise-aware logistic, scale 1 | 0.958 | 0.391 | 0.099 |
| noise-aware logistic, scale 3 | 1.0 | 0.028 | 0.324 |
| clean-only logistic, scale 1 | 1.0 | 0.045 | 0.288 |
| clean-only logistic, scale 3 | 1.0 | 0.0 | 0.402 |
- Scale 1 is the conditional: with the exact classifier the samples matched the class-1 conditional within sampling noise.
- Fidelity costs diversity: at scale 3 and above every sample was class 1, but the ambiguous mode almost vanished, so the sample distribution was a third wrong (TV ≈ 0.33).
- Clean-only classifier: its accuracy on noisy inputs was the same as the noise-aware one (0.934 against 0.936 at t = 100, 0.753 against 0.758 at t = 400), yet its mean gradient norm at t = 600 was 5.376 against 0.235 for the exact classifier, about 23 times too large. At scale 1 it already removed the central mode like strong guidance. Accuracy did not reveal the problem; calibration under noise did.
- Seeds 1 and 2: exact scale 1 TV 0.025 and 0.062; clean-only gradient norm 6.777 and 3.68; clean-only central share at scale 1 0.047 and 0.035.
- Caveat: an exact mixture score, a two-class toy and a linear-in-features classifier; real classifiers are networks whose gradients also contain adversarial directions.
Guidance in AI, robotics and aerospace
- Inverse problems: the same gradient term with a measurement likelihood in place of a classifier reconstructs images from sparse or noisy sensors, such as compressed satellite imagery or undersampled radar.
- Constraint and reward guidance: planners built on diffusion (Diffuser, Janner et al., 2022) add the gradient of a cost or reward to steer trajectories towards goals and away from obstacles.
- Safety filters: guidance by a classifier of unsafe content is one way to push generations away from a region without retraining.
References
- Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon and B. Poole (2021). “Score-based generative modeling through stochastic differential equations.” ICLR.
- P. Dhariwal and A. Nichol (2021). “Diffusion models beat GANs on image synthesis.” NeurIPS.
- J. Ho and T. Salimans (2022). “Classifier-free diffusion guidance.” arXiv:2207.12598.
- H. Chung, J. Kim, M. T. McCann, M. L. Klasky and J. C. Ye (2023). “Diffusion posterior sampling for general noisy inverse problems.” ICLR.
- A. Bansal et al. (2023). “Universal guidance for diffusion models.” ICLR.