Skip to content

Classifier-Free Guidance

A trick that makes diffusion models follow the prompt more closely by comparing a prompted and an unprompted prediction and pushing further in the difference.

Also CFG · guidance scale · negative prompt · conditional diffusion

Fig. / How it works
Noisy imageNoisy imagePredict with and without the promptPredict with and without the promptTake the differenceTake the differenceStep further in that directionStep further in that direction
Domain 06
Generative AI
Role · kind
Using and applyingTechnique
Level
Advanced

Ho and Salimans, 2022

Ask an artist to draw 'a dog', then ask them to draw 'anything'. Whatever is different between the two sketches is the 'dog-ness'. Exaggerate that difference and the drawing becomes more clearly a dog.

Where it fits

Diffusion ModelDiffusion ModelClassifier-Free Guidance
+All relationships as a list

Is a kind of / part of

Applies to

See it on the full map

Try it

From noise to data: diffusion, exactly
Loading interactive widget…
TakeawayDestroying data with noise is easy and needs no learning. A diffusion model learns to undo it one small step at a time, and guidance pushes each step harder toward what you asked for.

How it works

A diffusion model conditioned on a prompt sometimes produces images that only loosely match it. Classifier-free guidance sharpens the match.

During training, the prompt is randomly dropped for some examples, so the same network learns both:

  • a conditional prediction: “how to denoise toward a corgi on a skateboard”, and
  • an unconditional prediction: “how to denoise toward any image”.

At every denoising step, take the difference between the two. That difference points toward “more like the prompt”. Then move further in that direction, scaled by the guidance scale ww:

ε^=ε∅+w (εc−ε∅).\hat\varepsilon = \varepsilon_\varnothing + w\,(\varepsilon_c - \varepsilon_\varnothing).

  • w=1w = 1: plain conditional generation.
  • ww around 5 to 10: typical for text-to-image; much better prompt adherence.
  • Very high ww: harsh, oversaturated images that all look alike.

The widget uses this exact formula on a toy 2D dataset: raise the guidance and watch the samples crowd into the chosen class.

It plays a similar role to temperature in sampling from language models: a dial between creativity and faithfulness. Negative prompts reuse the same formula with an unwanted description in place of the empty prompt.

Everyday use cases

  • Case 01

    The guidance slider in image generators

    The "CFG scale" setting in Stable Diffusion tools is this technique.

  • Case 02

    Negative prompts

    Telling the model what not to draw ("blurry, extra fingers") works by guiding away from that description.

Strengths, weaknesses and when to use it

Strengths

  • Simple, needs no separate classifier, dramatically improves prompt following.

Weaknesses

  • Two network evaluations per step; high scales reduce diversity and look over-processed.

When to use itStandard in text-to-image and text-to-video diffusion. Tune the scale per model.

Compare with

Check yourself

  1. Q01

    What usually happens if you raise the guidance scale a lot?

    Show answer

    A: Images follow the prompt more but become less diverse and can look oversaturated

Go deeper