TL;DR
Classifier guidance makes a Diffusion Model generate images of a chosen class. At each denoising step, a separate classifier is asked how likely the noisy image is to belong to that class, and its gradient nudges the sample toward that class. A single scale number controls how hard the nudge is. In the 2021 ImageNet experiments it gave sharper class-conditional samples than the plain conditional model.
Builds on: Diffusion Model, Score Function, Softmax, Bayes' Rule · Leads to: Classifier-Free Guidance, which removes the separate classifier by training one network with and without the condition.
Where it's used
- Class-conditional image generation. In 2021, Dhariwal and Nichol used classifier guidance on ImageNet to make class-specific images with a diffusion model. Their guided models beat the best GAN results of that time on the image quality measures the paper reports, which was the result that made diffusion models widely noticed. See the ADM paper.
- Controllable sampling from a pretrained model. Because the classifier is trained separately, one generative model can be steered toward many classes without retraining it. You only need a new classifier.
- Research on guidance itself. The idea of adding a gradient toward a target to a sampler is the template for later methods, such as guiding by a detector, a segmentation model, or a reward. Classifier guidance is the simplest version, so it is the usual starting point.
How it works
Think of sampling as walking downhill through noise. Each step, the model says "the image should look like this", and the classifier adds "and it should look more like class 3". Here is one step on a one-number toy, so every number can be checked by hand.
- Start with a noisy value . The toy stands in for a noisy image, and we want the sample to end up in the region of class .
- The diffusion model says where the noisy data is likely to be, through its score. For this point the score is , which pulls toward 0.
- The classifier looks at and says "class is more likely if grows". Its gradient is .
- Choose a scale . With , the guided score is . The step size is , so moves by and lands at .
- Check: , and .
- With , the guided score is , so moves up to . The classifier's push now beats the diffusion model's pull, and the sample is steered toward class .
- Interpretation: sets how much the class gets to win over the data prior. Larger gives more class-specific samples and less variety.
The step in a diagram:
flowchart LR A[Noisy sample z_t] --> B[Diffusion model: score of p z] A --> C[Classifier: gradient of log p c given z] B --> D[Add s times the classifier gradient] C --> D D --> E[Reverse update to z_t-1] E --> A
The math
Step 2 in symbols: the score of the noisy data
Let be the distribution of noisy data at some noise level, and its score, as defined in Score Function. The diffusion model estimates this score at each step. Most diffusion models are trained to predict the noise that was added, not the score directly. At noise level the two are tied by , so the score is the predicted noise rescaled.
Step 3 in symbols: the classifier gradient
A classifier trained on noisy inputs produces logits for each class. Its probabilities come from Softmax:
The gradient is taken with respect to the noisy input. It is what the classifier contributes at each step. It is usually trained with Cross-Entropy Loss on noised images.
Step 4 in symbols: the guided score
Bayes' rule, from Bayes' Rule, gives the posterior for class :
Take the log and the gradient. The constant drops out, and we get
Classifier guidance adds a scale to the classifier term, and samples with the guided score
With this is the exact posterior score. With the class term is amplified, which is the sharpening described in the walkthrough. In the walkthrough, and are the two cases compared, so the reader can see the effect of the scale.
Step 5 in symbols: the update
The sampler moves by the guided score. In plain form, one Langevin-style step with step size and fresh noise is
as in Langevin Dynamics. Diffusion samplers use a discrete version of the same idea, with the step set by the noise schedule. The guided score replaces the usual one in that update.
Check the walkthrough with this formula: . With and no noise, .
Worked examples
Tiny. At a point , the diffusion score is and the classifier gradient is .
- With (no guidance), the guided score is .
- With , it is .
- With , it is .
- Each unit of adds exactly the classifier gradient, which is 1 here.
- Interpretation: the classifier's share of the push grows linearly with , and the data's share stays fixed. Large is what makes class-specific samples sharper and less varied.
Standard. Use the numbers from the walkthrough, with a step size and no noise.
- Start at with and .
- With , the score is . The update is .
- With , the score is . The update is .
- Compare: the two scales move in opposite directions, by 0.05 each way.
- Interpretation: when the classifier pulls harder than the data prior pushes back, a large enough reverses the direction of the step.
Realistic (a hypothetical budget, not a measured benchmark). A class-conditional diffusion sampler uses 50 denoising steps. Each step needs one forward and one backward pass through a classifier, and we assume one such pass takes 4 milliseconds.
- Extra time per step is one classifier forward and backward, so 4 ms.
- Over 50 steps, the extra time is ms per image.
- The diffusion network's own cost is unchanged, so the guided sampler is slower by the classifier's cost alone.
- Interpretation: the classifier adds a fixed cost per step, and that cost multiplies with the step count. Fewer steps make guidance cheaper.
Try it: a sampler uses 250 steps, and the classifier takes 4 ms per forward and backward pass. How many extra milliseconds does guidance add per image? Answer: ms, which is one second per image.
Common mistakes and tradeoffs
It is tempting to think the classifier just tells the model what to draw. In fact it only supplies a gradient, and the update still follows the diffusion model's score. The data prior keeps the sample looking like the training images, and the classifier only tilts it.
It is tempting to think a larger scale always gives better images. In fact a large can push samples into unrealistic regions, where the classifier is confident but the image looks wrong. It also cuts variety, because all samples are pushed toward the most class-typical features. Good scales are found by checking quality and diversity together.
It is tempting to think any classifier will do. In fact the classifier must work on noisy inputs at every noise level. A classifier trained only on clean images gives gradients that are unreliable at high noise, where most of the guidance happens.
Tradeoffs to keep in mind:
- When to use it: when you already have a good classifier, when you want to steer a fixed generative model toward new classes, or when you want a clear, separate control signal.
- When not to use it: when there is no classifier for the condition, such as free-form text, or when training a second network is too costly. Then Classifier-Free Guidance is the usual choice, because it trains one network with and without the condition and needs no separate classifier.
- Alternatives: classifier-free guidance, which replaces the classifier gradient with a difference between two predictions of the same network.
History
Diffusion models began as a way to learn to reverse a noising process, and early samples were not as sharp as those from GANs. Score-based and diffusion models were improving quickly by 2020, but conditional sampling was the open issue: how to make a model follow a class or a label without retraining it for each one.
Dhariwal and Nichol showed in 2021 that a classifier trained on noisy images could steer the sampler. Their paper reported that guided diffusion models beat the best GAN results on the image quality measures they used. It caught on because it gave a simple knob, the scale, that traded variety for fidelity, and because it used a plug-in classifier that could be changed without retraining the generator. Ho and Salimans then removed the classifier in 2022, which led to classifier-free guidance.
Recap
- A diffusion model's score says where the data is likely to be, and a classifier's gradient says where a class is likely to be.
- Classifier guidance adds the scaled classifier gradient to the diffusion score, with scale .
- With the guided score is the exact posterior score. Larger gives sharper, less varied class samples.
- The classifier must be trained on noisy images, and it adds a cost on every step.
- Read next: Classifier-Free Guidance for the version that drops the classifier, and the ADM paper for the experiments that made the method widely known.
Check yourself
QuestionWhat does the diffusion model's score contribute in classifier guidance, and what does the classifier's gradient contribute?Show answer
The diffusion model's score pulls the sample toward regions where noisy data is likely, which keeps the image realistic. The classifier's gradient tilts the sample toward regions where the chosen class is likely. The sampler uses the sum, so the data prior is kept and the class is added on top.
QuestionAt a point, the diffusion score is and the classifier gradient is . What is the guided score with , and which way does the sample move?Show answer
The guided score is . It is positive, so the sample moves in the positive direction, toward larger , even though the data prior alone would move it the other way.
QuestionWhy does the classifier have to be trained on noisy inputs rather than on clean images?Show answer
The sampler applies the classifier to noisy samples at every noise level, including the high-noise steps where most of the guidance happens. A classifier trained only on clean images gives unreliable gradients on noisy inputs, so the push toward the class would be unreliable where it matters most.
QuestionA guided sampler gives very sharp class samples, but all of them look almost the same. What scale change would you try first, and what would you check?Show answer
Lower the scale , because large pushes all samples toward the most typical class features and cuts variety. Check both sample quality and diversity at each scale, so the change does not trade one for the other too far.
Sources
- Dhariwal and Nichol, "Diffusion Models Beat GANs on Image Synthesis," 2021. https://arxiv.org/abs/2105.05233
- Ho and Salimans, "Classifier-Free Diffusion Guidance," 2022. https://arxiv.org/abs/2207.12598
- Ho, Jain and Abbeel, "Denoising Diffusion Probabilistic Models," 2020. https://arxiv.org/abs/2006.11239