Search

Search papers and lessons

Classifier Guidance

Level 4 · 10 min read

Classifier guidance steers a diffusion model's sampling toward a class by adding the gradient of a separately trained classifier's log-probability at each denoising step.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Inference
  • Generative models
  • ReadReady to readNot yet

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.

  1. Start with a noisy value z=1.0z = 1.0. The toy stands in for a noisy image, and we want the sample to end up in the region of class cc.
  2. The diffusion model says where the noisy data is likely to be, through its score. For this point the score is ∇log⁡p(z)=−1.0\nabla \log p(z) = -1.0, which pulls zz toward 0.
  3. The classifier looks at zz and says "class cc is more likely if zz grows". Its gradient is ∇log⁡p(c∣z)=+0.5\nabla \log p(c \mid z) = +0.5.
  4. Choose a scale ss. With s=1s = 1, the guided score is −1.0+1⋅0.5=−0.5-1.0 + 1 \cdot 0.5 = -0.5. The step size is 0.10.1, so zz moves by 0.1⋅(−0.5)0.1 \cdot (-0.5) and lands at 0.950.95.
  5. Check: −0.5⋅0.1=−0.05-0.5 \cdot 0.1 = -0.05, and 1.0−0.05=0.951.0 - 0.05 = 0.95.
  6. With s=3s = 3, the guided score is −1.0+3⋅0.5=0.5-1.0 + 3 \cdot 0.5 = 0.5, so zz moves up to 1.051.05. The classifier's push now beats the diffusion model's pull, and the sample is steered toward class cc.
  7. Interpretation: ss sets how much the class gets to win over the data prior. Larger ss 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 p(z)p(z) be the distribution of noisy data at some noise level, and ∇zlog⁡p(z)\nabla_z \log p(z) 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 ε\varepsilon that was added, not the score directly. At noise level σ\sigma the two are tied by ∇zlog⁡p(z)=−ε/σ\nabla_z \log p(z) = -\varepsilon / \sigma, so the score is the predicted noise rescaled.

Step 3 in symbols: the classifier gradient

A classifier trained on noisy inputs produces logits fc(z)f_c(z) for each class. Its probabilities come from Softmax:

p(c∣z)=exp⁡fc(z)∑jexp⁡fj(z).p(c \mid z) = \frac{\exp f_c(z)}{\sum_j \exp f_j(z)}.

The gradient ∇zlog⁡p(c∣z)\nabla_z \log p(c \mid z) 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 cc:

p(z∣c)∝p(z) p(c∣z).p(z \mid c) \propto p(z)\, p(c \mid z).

Take the log and the gradient. The constant drops out, and we get

∇zlog⁡p(z∣c)=∇zlog⁡p(z)+∇zlog⁡p(c∣z).\nabla_z \log p(z \mid c) = \nabla_z \log p(z) + \nabla_z \log p(c \mid z).

Classifier guidance adds a scale s≥0s \ge 0 to the classifier term, and samples with the guided score

s~(z)=∇zlog⁡p(z)+s ∇zlog⁡p(c∣z).\tilde s(z) = \nabla_z \log p(z) + s\, \nabla_z \log p(c \mid z).

With s=1s = 1 this is the exact posterior score. With s>1s \gt 1 the class term is amplified, which is the sharpening described in the walkthrough. In the walkthrough, s=1s = 1 and s=3s = 3 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 η\eta and fresh noise ξ\xi is

z←z+η s~(z)+2η ξ,z \leftarrow z + \eta\, \tilde s(z) + \sqrt{2\eta}\, \xi,

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: s~(1.0)=−1.0+3⋅0.5=0.5\tilde s(1.0) = -1.0 + 3 \cdot 0.5 = 0.5. With η=0.1\eta = 0.1 and no noise, z←1.0+0.1⋅0.5=1.05z \leftarrow 1.0 + 0.1 \cdot 0.5 = 1.05.

Worked examples

Example
The scale changes the push

Tiny. At a point zz, the diffusion score is ∇log⁡p(z)=2\nabla \log p(z) = 2 and the classifier gradient is ∇log⁡p(c∣z)=1\nabla \log p(c \mid z) = 1.

  1. With s=0s = 0 (no guidance), the guided score is 2+0⋅1=22 + 0 \cdot 1 = 2.
  2. With s=1s = 1, it is 2+1⋅1=32 + 1 \cdot 1 = 3.
  3. With s=2s = 2, it is 2+2⋅1=42 + 2 \cdot 1 = 4.
  4. Each unit of ss adds exactly the classifier gradient, which is 1 here.
  5. Interpretation: the classifier's share of the push grows linearly with ss, and the data's share stays fixed. Large ss is what makes class-specific samples sharper and less varied.
Example
The guided update on a toy

Standard. Use the numbers from the walkthrough, with a step size η=0.1\eta = 0.1 and no noise.

  1. Start at z=1.0z = 1.0 with ∇log⁡p(z)=−1.0\nabla \log p(z) = -1.0 and ∇log⁡p(c∣z)=0.5\nabla \log p(c \mid z) = 0.5.
  2. With s=1s = 1, the score is −1.0+0.5=−0.5-1.0 + 0.5 = -0.5. The update is z←1.0+0.1⋅(−0.5)=0.95z \leftarrow 1.0 + 0.1 \cdot (-0.5) = 0.95.
  3. With s=3s = 3, the score is −1.0+1.5=0.5-1.0 + 1.5 = 0.5. The update is z←1.0+0.1⋅0.5=1.05z \leftarrow 1.0 + 0.1 \cdot 0.5 = 1.05.
  4. Compare: the two scales move zz in opposite directions, by 0.05 each way.
  5. Interpretation: when the classifier pulls harder than the data prior pushes back, a large enough ss reverses the direction of the step.
Example
The cost of guidance in a realistic sampler

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.

  1. Extra time per step is one classifier forward and backward, so 4 ms.
  2. Over 50 steps, the extra time is 50⋅4=20050 \cdot 4 = 200 ms per image.
  3. The diffusion network's own cost is unchanged, so the guided sampler is slower by the classifier's cost alone.
  4. 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: 250⋅4=1000250 \cdot 4 = 1000 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 ss 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 ss.
  • With s=1s = 1 the guided score is the exact posterior score. Larger ss 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 −2-2 and the classifier gradient is +1+1. What is the guided score with s=3s = 3, and which way does the sample move?Show answer

The guided score is −2+3⋅1=1-2 + 3 \cdot 1 = 1. It is positive, so the sample moves in the positive direction, toward larger zz, 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 ss, because large ss 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