Search

Search papers and lessons

Classifier-Free Guidance

Level 4 · 12 min read

Classifier-free guidance steers a diffusion model toward a condition by extrapolating away from its unconditional prediction, with no separate classifier to train.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Generative models
  • Discrete math
  • Inference
  • Sign in to see your progress

TL;DR

Classifier-free guidance makes a conditional diffusion model follow its condition more strongly, such as a text prompt. At each denoising step the model makes two predictions, one with the condition and one without. The sampler then moves past the conditional prediction, in the direction that the condition adds. A single number, the guidance weight, controls how far it moves.

The method needs no separate classifier. One network learns both the conditional and the unconditional behaviour, which is why it is simple to train and to scale.

Where you'll see it: in text-to-image generators such as Stable Diffusion, where the "guidance scale" slider is this weight, and in most text-conditioned image and video models built since 2022.

Intuition

Turning up the accent

Suppose you ask a painter for "a red barn" and they paint from a loose memory of barns. The result is plausible but vague. Now suppose you could ask the painter two things: draw a barn in general, and draw a barn that is specifically red. The difference between those two drawings is exactly what "red" adds. If you then push the painting further in that direction, the red becomes more pronounced than the painter would normally let it be.

That is the whole idea. A conditional model and an unconditional model disagree about the image, and the disagreement points toward the condition. Classifier-free guidance takes a step in that direction and overshoots a little on purpose.

Why overshoot? A diffusion model trained on a condition is trained to match the data distribution, and that distribution is broad. Samples from it are faithful to the whole distribution but not always to the prompt. Sharpening the sampling toward the condition gives up some diversity to get closer to what was asked for. This trade is the source of both the benefit and most of the failure modes below.

flowchart LR
    Z[noisy image z_t] --> U[unconditional prediction]
    Z --> C[conditional prediction given prompt]
    U --> D[difference: what the prompt adds]
    C --> D
    D --> G[push past conditional by weight w]
    U --> G
    G --> S[next denoising step]

Both predictions come from the same network. The "unconditional" branch is the same model run with the condition switched off.

−3−2−101234500.10.20.30.40.5xdensity
  • unconditional, mean 0
  • conditional, mean 1
  • guided, w = 2, mean 3
In this toy one-dimensional case the guided distribution keeps the conditional spread but moves its centre past the conditional mean, by $w$ times the gap between the conditional and unconditional means.

The math

Two predictions from one network

Diffusion models predict a quantity at each noise level tt from the noisy input ztz_t. The common choice is the noise εθ(zt,t,c)\varepsilon_\theta(z_t, t, c), where cc is the condition, such as a text embedding. The same network can be run with the condition removed. During training, the condition is dropped at random for a fraction of examples, so the network learns both behaviours. The original paper compares dropout rates of 0.1, 0.2 and 0.5 and finds that 0.5 is consistently worse than 0.1 or 0.2, which perform about equally. The unconditional prediction is written εθ(zt,t,∅)\varepsilon_\theta(z_t, t, \varnothing).

Here ∅\varnothing is a null token that stands for "no condition".

The guided prediction

The guided prediction is

ε~θ(zt,t,c)=(1+w) εθ(zt,t,c)−w εθ(zt,t,∅).\tilde\varepsilon_\theta(z_t, t, c) = (1 + w)\,\varepsilon_\theta(z_t, t, c) - w\,\varepsilon_\theta(z_t, t, \varnothing).

Here w≥0w \ge 0 is the guidance weight. Setting w=0w = 0 gives the plain conditional model. Larger ww moves further from the unconditional prediction and past the conditional one.

Rearranged, this is the conditional prediction plus ww times the difference:

ε~θ=εθ(zt,t,c)+w (εθ(zt,t,c)−εθ(zt,t,∅)).\tilde\varepsilon_\theta = \varepsilon_\theta(z_t, t, c) + w\,\big(\varepsilon_\theta(z_t, t, c) - \varepsilon_\theta(z_t, t, \varnothing)\big).

The bracketed difference is the direction the condition adds. The weight scales it.

Why this is a classifier in disguise

The first form of the method came from classifier guidance, where a separate classifier p(c∣z)p(c \mid z) is trained on noisy images and its gradient steers sampling. Bayes' rule, from Bayes' Rule, gives the posterior as p(z∣c)∝p(z) p(c∣z)/p(c)p(z \mid c) \propto p(z)\, p(c \mid z) / p(c). Taking logarithms and gradients and raising the classifier term to a power ww gives a sampling score of the form

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

The gradient of the log classifier equals the difference of the two scores, ∇zlog⁡p(c∣z)=∇zlog⁡p(z∣c)−∇zlog⁡p(z)\nabla_z \log p(c \mid z) = \nabla_z \log p(z \mid c) - \nabla_z \log p(z). Substituting it in gives (1+w)∇zlog⁡p(z∣c)−w ∇zlog⁡p(z)(1+w)\nabla_z \log p(z \mid c) - w\,\nabla_z \log p(z). This is the score form of the same rule. Because the diffusion network already estimates the scores, a separate classifier is not needed. The conversion from score to noise is the step-by-step relation described in Score Matching and Diffusion Model.

Guided sampling

Each denoising step computes ε~\tilde\varepsilon and uses it in place of the usual prediction in the reverse update. Two network passes are needed per step, one conditional and one unconditional. In practice the two inputs are batched together, so the cost is roughly double the cost of plain sampling.

Worked examples

Example
One scalar step with three weights

Take a single number, with conditional prediction εθ(z,c)=0.8\varepsilon_\theta(z, c) = 0.8 and unconditional prediction εθ(z,∅)=0.2\varepsilon_\theta(z, \varnothing) = 0.2.

  1. With w=0w = 0: (1)(0.8)−0(0.2)=0.8(1)(0.8) - 0(0.2) = 0.8. This is the plain conditional prediction.
  2. With w=1w = 1: (2)(0.8)−(1)(0.2)=1.6−0.2=1.4(2)(0.8) - (1)(0.2) = 1.6 - 0.2 = 1.4.
  3. With w=3w = 3: (4)(0.8)−(3)(0.2)=3.2−0.6=2.6(4)(0.8) - (3)(0.2) = 3.2 - 0.6 = 2.6.
  4. Check the rearranged form at w=3w = 3: 0.8+3(0.8−0.2)=0.8+1.8=2.60.8 + 3(0.8 - 0.2) = 0.8 + 1.8 = 2.6. It agrees.
  5. Interpretation: the gap between the two predictions is 0.60.6. Each unit of weight adds one more gap to the conditional prediction. With w=3w = 3 the guided value is 2.6−0.2=2.42.6 - 0.2 = 2.4 above the unconditional prediction, four times the original gap of 0.60.6 (in general (1+w)(1+w) times the gap), which is the sharpening effect in action.
Example
A Gaussian where the answer is known

Let the conditional distribution be p(x∣c)=N(1,1)p(x \mid c) = \mathcal{N}(1, 1) and the unconditional distribution be p(x)=N(0,1)p(x) = \mathcal{N}(0, 1). Their scores are ∇xlog⁡p(x∣c)=−(x−1)\nabla_x \log p(x \mid c) = -(x - 1) and ∇xlog⁡p(x)=−x\nabla_x \log p(x) = -x.

  1. Apply the guided rule with w=2w = 2: (1+w)∇log⁡p(x∣c)−w ∇log⁡p(x)=3 (−(x−1))−2 (−x)(1+w)\nabla \log p(x \mid c) - w\,\nabla \log p(x) = 3\,(-(x-1)) - 2\,(-x).
  2. Expand: −3x+3+2x=−x+3-3x + 3 + 2x = -x + 3.
  3. Write this as −(x−3)-(x - 3). A score of the form −(x−μ)-(x - \mu) belongs to N(μ,1)\mathcal{N}(\mu, 1), so the guided distribution is N(3,1)\mathcal{N}(3, 1).
  4. Interpretation: the conditional mean was 11 and the unconditional mean was 00. Guidance with w=2w = 2 moved the centre to 33, which is the conditional mean plus ww times the gap. The spread stays at 11. Sampling from the guided distribution gives samples that sit further toward the condition than the conditional distribution itself does.

Tradeoffs and failure modes

Guidance buys prompt adherence at the cost of diversity and, past a point, image quality. The weight is a single knob with a sweet spot.

SituationProblemBetter choice
Weight too lowOutput ignores the prompt or drifts from itRaise ww toward the usual range
Weight too highOversaturated colours, harsh contrast, repeated texturesLower ww, or use dynamic thresholding
Diversity matters more than fidelityEvery sample looks alike at high wwKeep ww small, or sample with w=0w = 0
Real-time generationTwo passes per step double the costDistilled or few-step samplers, or guidance distillation
Unconditional quality mattersThe null-condition branch is undertrainedTune the condition dropout rate

The most common failure is over-saturation. Because the step overshoots, pixel values can leave their normal range, and the output looks burnt. Practitioners clip the predicted image, or use dynamic thresholding that rescales it per sample, to keep the overshoot under control.

Guidance also assumes that the difference between the two predictions means something. When the condition is weak or ambiguous, the difference is mostly noise, and high weights amplify the noise.

History

In 2021, Dhariwal and Nichol introduced classifier guidance for diffusion models. They trained a classifier on noisy images and used its gradient, which gave a clear quality gain on class-conditional ImageNet generation. Its drawback was that the classifier had to be trained on noisy inputs, which added a second model and training pipeline.

Ho and Salimans (arXiv, 2022) showed that the classifier could be dropped. A single conditional model, trained with the condition sometimes removed, gives both predictions the rule needs. Their paper framed the method as classifier-free guidance and reported that it trades off sample fidelity (Inception score) against diversity and FID in a manner similar to classifier guidance, on class-conditional ImageNet at 64x64 and 128x128, without the extra model.

It caught on because it was simple, it worked for any condition that could be fed to the network, and it suited text. Later text-to-image systems adopted it as a default, with guidance scales that users could adjust directly. Its use in text-conditioned models is the reason the term is now standard.

Check yourself

QuestionRecall: what does the guidance weight w=0w = 0 reduce the guided prediction to?Show answer

With w=0w = 0 the formula is (1+0) εθ(zt,t,c)−0=εθ(zt,t,c)(1+0)\,\varepsilon_\theta(z_t, t, c) - 0 = \varepsilon_\theta(z_t, t, c). The guided prediction is just the plain conditional prediction. No extra guidance is applied, and the sampler behaves like an ordinary conditional diffusion model.

QuestionComputation: with conditional prediction 0.50.5, unconditional prediction −0.1-0.1, and w=4w = 4, what is the guided prediction?Show answer

Use (1+w)εc−w εu=5(0.5)−4(−0.1)=2.5+0.4=2.9(1+w)\varepsilon_c - w\,\varepsilon_u = 5(0.5) - 4(-0.1) = 2.5 + 0.4 = 2.9. Check with the rearranged form: 0.5+4(0.5−(−0.1))=0.5+4(0.6)=0.5+2.4=2.90.5 + 4(0.5 - (-0.1)) = 0.5 + 4(0.6) = 0.5 + 2.4 = 2.9. The answer is 2.92.9.

QuestionWhy: why does the method need the condition to be dropped sometimes during training?Show answer

The method needs a prediction without the condition at sampling time. If the network had only ever seen conditioned inputs, it would have no learned unconditional behaviour, and the second prediction would be meaningless. Randomly dropping the condition for a fraction of training examples teaches one network both behaviours, so no separate model is required.

QuestionApplied: a product team's images look correct but are washed out at a high weight, and they want a faster sampler for a mobile app. What should they check first?Show answer

They should first check the weight, because high values overshoot and push pixel values out of range, which can look oversaturated or harsh. They can lower ww or use clipping or dynamic thresholding. For speed, guidance costs two passes per step, so a distilled model that folds the guidance into one pass, or a few-step sampler, is the usual route. The two problems have different fixes, so they should be diagnosed separately.

Sources