Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
Classifier-free
guidance.
Every diffusion image tool has a slider labeled "guidance scale" or "CFG." Turn it up and the picture snaps to your prompt — sharper, more literal, less wandering. Turn it down and you get variety, sometimes vagueness. That slider is one of the most-used knobs in generative AI, and it comes from a strikingly simple idea: make the model predict the noise twice — once told your prompt, once told nothing — and then extrapolate along the difference. No extra classifier, no extra network. Just two predictions and a weighted subtraction. Here's the formula behind the knob.
What changes against conditional diffusion: the prompt gets a volume knob
A diffusion model conditioned on text will follow the prompt — loosely. Samples drift toward the general shape of the data rather than the specific thing asked for, because nothing in the objective rewards obedience over plausibility. The existing fix was classifier guidance: train a separate classifier that works on noisy images, and use its gradient to push each denoising step toward the desired class. It works, and it requires training and maintaining a noise-aware classifier, which is a second model and an awkward one.
The change removes it, and the trick is to get two models out of one. During training, drop the condition some fraction of the time — replace the prompt with nothing — so the same network learns to denoise both conditionally and unconditionally. At sampling time, run it twice and compare: the difference between the conditional prediction and the unconditional one is the direction that the prompt is asking for. Then extrapolate along it, going further than the conditional prediction alone, by a factor you choose.
That factor is the guidance scale, and stated in units, it is the single knob every user of every image model turns. At one, you get ordinary conditional sampling. Higher, and samples obey the prompt more insistently. The cost is exactly one thing and worth knowing: each step now costs two network evaluations instead of one, because the model must be run with and without the condition.
The guidance knob
A diffusion model generates by starting from pure noise and, step by step, predicting and removing a little noise until an image emerges. To make it follow a prompt, you condition that noise prediction on the text. But conditioning alone is often too gentle — the model drifts, hedges, produces something loosely on-topic. You want a dial: how hard should it obey the prompt?
The first answer, classifier guidance, borrowed a separate image classifier and pushed each denoising step toward whatever the classifier thought matched the label — effective, but it meant training and running an extra network on noisy images, an ungainly failure-prone add-on. Classifier-free guidance (Ho & Salimans, 2021) got the same dial with no extra model at all. The trick: train the one diffusion model to work both with the prompt and without it, then at sampling time ask it for both predictions and combine them. That combination is the guidance knob, and it's now in essentially every text-to-image and text-to-video system.
Predict the noise twice — conditioned on the prompt (ε_cond) and unconditioned (ε_uncond) — and extrapolate: ε = (1+w)·ε_cond − w·ε_uncond, where w is the guidance weight. w = 0 is plain conditioning; larger w pushes harder toward the prompt.
Two predictions
The prerequisite is a diffusion model that can run both ways. You get it almost for free during training: with some probability, randomly drop the condition — replace the text prompt with an empty/null token — so the same network learns to denoise both when told a prompt and when told nothing. After training, one network gives you two predictions on demand: ε_cond (the noise it expects given your prompt) and ε_uncond (the noise it expects given no prompt at all).
These two predictions point in slightly different directions. The unconditional one heads toward "any plausible image." The conditional one heads toward "a plausible image that matches the prompt." The difference between them, ε_cond − ε_uncond, is therefore the conditional direction — the part of the prediction that is specifically because of the prompt. Guidance is nothing more than deciding how far to travel along that difference vector. The whole method reduces to: get both predictions, take their difference, and amplify it.
Extrapolate the difference
Guidance starts from the unconditional prediction and steps past the conditional one — an extrapolation, not an interpolation. That "overshoot" is what makes prompt adherence stronger than plain conditioning:
Read it as: begin at the unconditional prediction, then travel (1+w) times the conditional-direction vector. At w = 0 you land exactly on ε_cond — normal conditioning. As w grows you shoot beyond it, exaggerating the very features the prompt is responsible for, which is why higher guidance gives tighter, more literal prompt-following. But it's an extrapolation, and extrapolating too far leaves the region where the model's predictions are reliable — hence the familiar failure of high guidance scales: blown-out colors, harsh contrast, and artifacts. The whole art of setting the slider is finding how far you can push before the extrapolation breaks.
The guidance formula
The guided noise prediction is a weighted combination of the two, with guidance weight w:
w = 0 gives ε_cond (plain conditioning); w = −1 gives ε_uncond (no prompt). For ε_cond = 2, ε_uncond = 1: at w = 3, ε = 4·2 − 3·1 = 5 — pushed well past the conditional value of 2.
The same thing, written as an extrapolation, makes the geometry explicit — start at the unconditional prediction, travel (1+w) along the conditional direction:
Identical to the first form — expand it and the ε_uncond terms recombine. The bracket ε_cond − ε_uncond is the conditional direction; (1+w) is how far you go. The runnable version below computes the guided prediction, shows the two forms agree, and sweeps the weight.
How classifier-free guidance trains one network and samples with two passes
Ho and Salimans start from classifier guidance (Dhariwal and Nichol, 2021), which mixes a diffusion model's score with the gradient of a separately trained classifier on noisy images, and ask whether the same effect, trading diversity for fidelity the way truncation does in GANs, can be had with no classifier at all. The answer has a training half and a sampling half. The paper works in continuous time, with noisy inputs zλ = αλ x + σλ ε indexed by the log signal-to-noise ratio λ, but the recipe carries over unchanged to discrete-step models.
- Give the model a null condition. Use one network εθ(zλ, c) and reserve a token ∅ meaning "no condition". The unconditional model is εθ(zλ, ∅); there is no second set of weights.
- Train with condition dropout. For each training example, with probability puncond replace the class label c by ∅, then take an ordinary diffusion step: noise the image to a random λ and regress the noise. The paper swept puncond over 0.1, 0.2 and 0.5.
- Run the network twice per step. At every sampling step, evaluate εθ(zλ, c) and εθ(zλ, ∅) on the same noisy input. Implementations usually batch the two, but it is still twice the compute.
- Combine. Form ε̃ = (1 + w) εθ(zλ, c) − w εθ(zλ, ∅) and hand ε̃ to the usual sampler in place of the conditional prediction. The paper swept w from 0 up to 4.
- Change nothing else. The experiments reused the architectures and hyperparameters of Dhariwal and Nichol's class-conditional ImageNet models at 64×64 and 128×128, even though they were tuned for classifier guidance, and altered only the training dropout and the sampling rule.
The convention trap: w versus guidance scale
The paper's weight w measures extra push beyond the conditional prediction, so w = 0 is plain conditional sampling. Most later papers and libraries, including GLIDE, Imagen and the diffusers implementation of Stable Diffusion, write the same rule as ε∅ + s(εc − ε∅) with a guidance scale s = 1 + w. Under that convention s = 1 is plain conditional sampling and s = 0 ignores the condition. A guidance scale of 7.5, the familiar Stable Diffusion default, is w = 6.5 in this paper's notation. Mixing the two conventions is the most common way to misread a guidance number.
Why it works without a classifier
Bayes' rule gives p(c | z) ∝ p(z | c)/p(z), so the gradient of an implicit classifier's log-probability is the conditional score minus the unconditional score, and a diffusion model's noise prediction is a scaled score. Classifier guidance with this implicit classifier therefore needs only the two predictions the model already makes. The authors add their own caveat: the networks are not exact scores, so the guided prediction is not, in general, the gradient of any actual classifier. The sampling target it approximates is p(z | c) times the implicit classifier's probability raised to the power w.
What the dropout rate controls
puncond sets the share of training spent on the unconditional task. A small share proved enough: 0.1 and 0.2 performed about the same, and 0.5 was worse.
Classifier-free guidance from Bayes’ rule, in both conventions
p(c | zλ)— the implicit classifier, never trained or evaluated; only its gradient appears, as a difference of two predictions.σλ— the noise scale at this level; dividing by it turns a noise prediction into a score.εθ(zλ)— the paper's shorthand for the unconditional prediction εθ(zλ, ∅).wands— the paper's weight (w = 0 is the conditional model) and the libraries' scale (s = 1 is the conditional model).
Extremes: w = −1 (s = 0) gives the unconditional model and w = 0 (s = 1) the conditional one. Written without the classifier, the target is p(z | c)^1+w / p(z)^w: the conditional model raised to a power above one and divided by the unconditional one, so as w grows it concentrates on the inputs that look most like the class, which is why diversity falls.
What Ho and Salimans measured on ImageNet
All experiments are class-conditional ImageNet at 64×64 and 128×128, using the architectures and hyperparameters of Dhariwal and Nichol's guided diffusion models, so the comparison with classifier guidance is like for like. Two knobs were swept, the training dropout puncond and the guidance weight w, with samples scored by FID (lower is better) and Inception Score (higher is better). The question was whether the FID–IS trade-off curve that classifier guidance and GAN truncation produce could be had without a classifier.
| Knob | Values tried | What happened |
|---|---|---|
| Guidance weight w | 0 to 4 | FID best at small weights, a few tenths, then rising; IS rising steadily with w |
| Unconditional dropout puncond | 0.1, 0.2, 0.5 | 0.1 and 0.2 about equal; 0.5 worse across the whole FID–IS frontier |
| Classifier versus none | Same architectures as classifier guidance | A similar FID–IS trade-off, with no classifier trained |
| Cost per sampling step | — | Two network evaluations instead of one |
The weight sweep is the main result and also the main negative one. A little guidance improves FID over the unguided model, because samples become more convincing faster than they lose variety. Beyond a few tenths, FID gets worse while Inception Score keeps climbing: samples look more like their class and less like the full data distribution. No single w optimizes both, which is why guidance became a user-facing setting rather than a fixed constant. The puncond result is quieter but useful: the unconditional model needs only a small slice of training, so the method costs almost nothing in capacity.
The paper names its own limits. Sampling runs the network twice per step, which it suggests might be reduced by architectures that inject the condition late, so the two passes share most of their computation. And any method that buys fidelity with diversity raises the question of whether the lost diversity is acceptable for a given use. Later, Meng and colleagues (2023) distilled guided models into a single pass per step.
What the authors admit, and what happened next
The trade is named in the paper and is visible in every image anyone has generated: guidance buys fidelity to the prompt at the cost of diversity. Turn it up and samples converge on a narrow, high-contrast, often over-saturated interpretation; turn it down and you get variety, including variety you did not want. There is no setting that is correct, which is why it is exposed to users.
The mechanism also explains why the artefacts look the way they do. The step is an extrapolation — it deliberately moves beyond what the model predicts — so at high scales it can leave the region the model was trained on entirely, and the results are not merely wrong but wrong in a characteristic, over-cooked way. Later work on clamping and rescaling the guided prediction is all about staying on the manifold while keeping the obedience.
How it held up: universally adopted, immediately, and still the default. It is the clearest case on this ladder of a technique whose contribution is control rather than capability — the model was already able to produce the image, and this is what lets someone ask for it. Worth noting how cheap the idea was: train with the condition dropped occasionally, which costs nothing, and the ability to dial conditioning strength falls out of arithmetic on two predictions.
The guidance knob, from scratch
Classifier-free guidance combines two noise predictions — conditional (given the prompt) and unconditional (given nothing) — as ε = (1+w)·ε_cond − w·ε_uncond. w=0 is plain conditioning; w=−1 recovers the unconditional prediction; larger w pushes further along the conditional direction (ε_cond − ε_uncond) for stronger prompt adherence. The weight form and the interpolation form ε_uncond + (1+w)·(ε_cond − ε_uncond) are the same thing. Change the two predictions and the weight and watch the guided output move.
Write the body of guidance_direction. Everything else in the file is
already written; this is the function the paper is about. Press ▶ Run to check it against
the 5 assertions below — they start red.
- W=0 recovers the plain conditional prediction
- W=-1 recovers the purely unconditional prediction
- Increasing w pushes further along the conditional direction (stronger guidance)
- The weight form and the interpolation form are the same thing
- The guidance direction is the conditional minus the unconditional prediction
The default knob
Classifier-free guidance is now the default way conditional diffusion models are steered. The "guidance scale" (CFG scale) in Stable Diffusion and every mainstream text-to-image tool is a rescaling of the w in this formula (tools usually expose s = 1 + w), and the same idea is widely used in text-to-video diffusion models. A huge fraction of the practical controllability people enjoy in image generation — the ability to say "follow my prompt more" and have it work — is this one weighted subtraction.
Its ubiquity also made its main cost worth attacking. Because guidance doubles the forward passes per step, later work on guidance distillation trained models to reproduce the guided output in a single pass, folding the effect into the network so you keep the control without the 2× compute — an important optimization for fast and real-time generation. Variants also refine where guidance is applied (only at certain noise levels) or how it's scheduled across the sampling trajectory. But the core object — two predictions, extrapolate along the difference — is remarkably stable, and it's one of those rare ideas that is both a one-line formula and a cornerstone of a whole generation of creative tools.
Guidance is where "negative prompts" come from. Instead of an empty unconditional prompt, you can feed a negative prompt as ε_uncond — then guidance pushes the sample away from that concept as it pushes toward the positive one. "Extra fingers, blurry" as a negative prompt is literally the unconditional term in the CFG formula, repurposed to steer away from unwanted features.
Questions people ask about Classifier-Free Guidance
How is classifier-free guidance derived from Bayes’ rule?
Classifier guidance samples from p(z|c) times p(c|z) raised to a power w, using the gradient of a trained classifier on noisy inputs. Bayes' rule gives p(c|z) proportional to p(z|c) divided by p(z), so the gradient of log p(c|z) is the conditional score minus the unconditional score. A diffusion model's noise prediction is the score scaled by minus the noise level, so that difference is just the conditional noise prediction minus the unconditional one, divided by the noise level. Substitute it into the classifier-guidance formula and the classifier disappears: the guided prediction is (1 + w) times the conditional prediction minus w times the unconditional one. The authors note the networks are not exact scores, so this is a well-motivated heuristic rather than an exact classifier gradient.
What does a CFG scale of 7.5 mean in Stable Diffusion?
Libraries such as diffusers compute the guided prediction as the unconditional prediction plus s times the difference between the conditional and unconditional predictions. A scale s of 7.5 therefore means the sampler travels seven and a half times the distance from the unconditional prediction to the conditional one, at every denoising step. In the classifier-free guidance paper's notation, which writes (1 + w) times conditional minus w times unconditional, that is w = 6.5, because s = 1 + w. At s = 1 you get plain conditional sampling and at s = 0 the prompt is ignored. In Stable Diffusion the unconditional branch is the empty prompt, or the negative prompt when one is given, so a negative prompt is literally the point the sampler pushes away from.
What conditioning dropout rate should you use to train for classifier-free guidance?
The paper replaced the condition with a null token with probability 0.1, 0.2 or 0.5 and found that 0.1 and 0.2 performed about equally well, while 0.5 was worse across the entire FID versus Inception Score frontier. The authors' reading is that the unconditional model needs only a small share of the network's capacity and training. Rates around 0.1 are common in text-to-image training today. Too low a rate leaves the unconditional branch undertrained, which makes the difference between the two predictions noisy; too high a rate starves the conditional model. The null token itself is a design choice: the embedding of an empty prompt in text-to-image models, a reserved class index in class-conditional ones.
Why do high classifier-free guidance scales oversaturate images?
Guidance is an extrapolation: it moves the noise prediction past the conditional prediction, away from the unconditional one, by a factor the user picks. Far enough out, the implied estimate of the clean image leaves the range the model saw in training, typically values beyond the data's bounds, and each later step compounds the error. The visible result is blown-out color, harsh contrast and overcooked texture. The paper documents the statistical side of the same trade: FID worsens and Inception Score rises as the weight grows, meaning samples become more class-typical and less diverse. Later systems added fixes; Imagen introduced dynamic thresholding, which clips and rescales the predicted clean image at each step, and other work rescales the guided prediction to keep its magnitude in range.
Why does classifier-free guidance double the cost of sampling, and how can that be avoided?
Every denoising step needs two predictions from the network, one with the condition and one with the null condition, so the network runs twice per step. Implementations batch the two inputs together, which uses the hardware well but still doubles the compute. The paper flags this as the method's practical disadvantage and suggests that injecting the condition late in the network could let the two passes share most of their work. The approach that took hold was distillation: Meng and colleagues in 2023 trained a student network to reproduce the guided output in one pass per step, taking the guidance weight as an input, and combined it with step distillation to cut the number of sampling steps as well.
4 questions to test what you just read
Work each one out before you open it — a number, a prediction or a reason, not a definition. Producing the answer is what makes it stick. Your progress is kept on this device.
With ε(z, c) = 0.8 and ε(z, ∅) = 0.2, what does a library guidance scale of 7.5 give, and what would you get by plugging 7.5 into the paper’s w instead?
Show answer
Library form: 0.2 + 7.5 × (0.8 − 0.2) = 4.7. Paper form with w = 7.5: 8.5 × 0.8 − 7.5 × 0.2 = 5.3, which is library scale 8.5. The same number means a different push depending on the convention, because s = 1 + w.
What does the guided target distribution become at w = 0, and what does it approach as w grows very large?
Show answer
At w = 0 the target p(z | c) p(c | z)^w is just p(z | c), ordinary conditional sampling. As w grows without limit it concentrates on inputs where the implicit classifier p(c | z) is most confident, so diversity collapses toward a few highly class-typical samples, the direction GAN truncation also moves.
Why might training with p_uncond = 0.5 hurt sample quality even though it trains the unconditional branch more than 0.2 does?
Show answer
The guided prediction leans hardest on the conditional model, weighted 1 + w. The paper's reading is that the unconditional task needs only a small share of capacity, so spending half of all training on it takes data and capacity from the conditional model it relies on. Result: 0.1 and 0.2 about equal, 0.5 worse everywhere.
How many network evaluations does a 50-step sampler with classifier-free guidance make, and does a negative prompt change that count?
Show answer
Two per step, so 100, usually run as 50 double-sized batches. A negative prompt changes nothing in the count: it replaces the empty prompt in the unconditional branch, so the sampler pushes away from it while pushing toward the positive prompt.
Generating pixels
Ten papers from the first latent-variable generator to video and real-time 3D. The diffusion half is strictly cumulative — each rung removes a cost the last one paid.
- 1VAE2014
- 2GANs2014
- 3VQ-VAE2017
- 4DDPM2020
- 5DDIM2021
- 6Classifier-Free Guidance2021
- 7Latent Diffusion2022
- 8Sora2024
- 9NeRF2020
- 103D Gaussian Splatting2023
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Paper Breakdowns
- Attention Is All You NeedThe paper that introduced the Transformer — the architecture underneath GPT, BERT, Claude, and Gemini. Self-attention, Query/Key/Value, multi-head attention, positional encoding, and the real BLEU results, explained in depth with analogies and animated visuals, not just equations.Read →
- Word2VecHow words became math. Dense word vectors learned from the company a word keeps, so king − man + woman lands near queen. Skip-gram, CBOW, the distributional hypothesis, and the origin of every embedding model powering search and RAG.Read →
- ResNetThe skip connection that made deep learning deep. Why stacking layers made networks worse, and how residual learning let them reach 150+ layers and win ImageNet — the identity shortcut every Transformer still uses inside every block.Read →
- Vision Transformer (ViT)When an image became 16×16 words. Cut a picture into patches, treat each patch as a token, and feed them to a plain Transformer — no convolutions. Patch embeddings, the class token, and why data scale lets attention beat the CNNs that ruled computer vision for a decade.Read →
- Latent DiffusionThe paper behind Stable Diffusion. Compress an image into a small latent space with a VAE, run the diffusion denoising there instead of on pixels for a tens-of-times efficiency win, and steer it with text via cross-attention. The architecture that put text-to-image generation on consumer GPUs.Read →
- DDPMThe original denoising diffusion math — the paper that made diffusion work. A fixed forward process adds Gaussian noise over many steps; a U-Net learns the reverse. The masterstroke: train the network to simply predict the added noise with a plain MSE loss. The foundation under every modern image generator.Read →