Conditional diffusion models (like text-to-image systems) often produce samples that are high-quality but don't strongly follow the conditioning signal (text, class label, etc.). Early approaches used a separate classifier gradient to steer generation, but this required training an extra noise-robust classifier.
Classifier-free guidance eliminates the classifier entirely by training a single model to handle both conditional and unconditional generation, then combining their predictions at sampling time.
We want to sample from p(x∣c) but make it more strongly reflect the conditioning c. In classifier guidance, we modified the score with ∇xtlogp(c∣xt). Here we'll derive an equivalent effect without a classifier.
STEP 2: Rewrite the conditional score
By Bayes' rule:
logp(xt∣c)=logp(xt)+logp(c∣xt)−logp(c)
WHY this step? The last term logp(c) doesn't depend on xt, so it vanishes under the gradient. This splits the conditional score into unconditional score + classifier gradient.
STEP 3: Amplify the conditioning signal
To make the sample follow c more strongly, we scale up the classifier term by a factor w:
∇xtlogp~(xt∣c)=∇xtlogp(xt)+w⋅∇xtlogp(c∣xt)
WHY? When w=1, we get the true conditional. When w>1, we "overshoot" toward the conditioning, making outputs more specific to c at the cost of diversity.
STEP 4: Express in terms of the model's predictions
Rearrange the Bayes gradient:
∇xtlogp(c∣xt)=∇xtlogp(xt∣c)−∇xtlogp(xt)
Substitute into the guided score:
∇xtlogp~(xt∣c)=∇xtlogp(xt)+w⋅[∇xtlogp(xt∣c)−∇xtlogp(xt)]
=(1−w)∇xtlogp(xt)+w∇xtlogp(xt∣c)
STEP 5: Convert to noise prediction
In diffusion models, the score relates to predicted noise: ∇xtlogp(xt)∝−ϵθ(xt,t). So:
ϵ~θ=(1−w)ϵθ(xt,t,∅)+wϵθ(xt,t,c)
WHY this form? It's a linear interpolation and extrapolation. When w=0, purely unconditional. When w=1, normal conditional. When w>1, we extrapolate beyond the conditional toward "more c."
To enable classifier-free guidance, we train a single model that handles both conditional and unconditional denoising:
During training, randomly drop the conditioning c with probability puncond (typically 10-20%).
When dropped, replace c with a null token ∅ (empty string for text, zero embedding, etc.).
Loss remains the same:
L=Et,x0,ϵ,c[∥ϵ−ϵθ(xt,t,c)∥2]
where c is sometimes ∅.
WHY this works: The model learns to denoise both with and without conditioning in the same parameters. At inference, we can call it twice (once with c, once with ∅) to get both predictions.
Score-based diffusion models: CFG manipulates the score function by combining conditional and unconditional scores.
Classifier guidance: The predecessor approach that used a separate classifier p(c∣xt); CFG derives the same effect without classifier.
DDPM sampling: CFG modifies the noise prediction ϵθ used in the DDPM reverse process.
Conditional generation: CFG is a technique to strengthen conditioning signals in conditional generative models.
Prompt engineering: Higher guidance scales make prompt wording more critical (overfitting to text).
Negative prompting: Often combined with CFG to push away from unwanted features.
Recall Explain It Like I'm 12
Imagine you're drawing a picture and someone gives you a hint: "draw a dragon." You can choose to follow that hint a little or a lot.
Classifier-free guidance is like having two artists in your brain:
One artist draws what they'd normally draw (no hint).
Another artist draws specifically following the "dragon" hint.
You look at both drawings, see the difference between them (that's the "dragon-ness"), and then you push your final drawing even MORE in that dragon direction. It's like someone saying "make it MORE dragon!" So you exaggerate the dragon features—bigger wings, fiercer claws—to make sure your picture really screams "DRAGON!"
The "guidance scale" (w) is how much you exaggerate. A little bit (w=2) gives you a normal dragon. A lot (w=10) gives you a SUPER obvious dragon, but maybe it looks a bit cartoonish because you pushed too hard.
You want strong adherence to text/class conditioning without training a separate classifier
You're building text-to-image, text-to-video, or class-conditional models
You need a tunable knob for prompt strength
Avoid or tune down when:
You want maximum sample diversity (high w causes mode collapse)
Compute cost is critical (requires 2× model evaluations per step)
You're generating unconditional samples (just use w=0 or skip CFG)
#flashcards/ai-ml
What is the core idea behind classifier-free guidance?
It amplifies conditioning by running the model twice—once with conditioning and once without—then extrapolating beyond the conditional prediction by scaling up the difference between them.
What is the CFG formula for guided noise prediction?
ϵ~θ=ϵθ(xt,t,∅)+w⋅[ϵθ(xt,t,c)−ϵθ(xt,t,∅)] or equivalently ϵ~θ=(1−w)ϵuncond+wϵcond
What does the guidance scale w control?
w controls how strongly the sample follows the conditioning. w=1 is normal conditional generation, w>1 amplifies prompt adherence, w=0 ignores conditioning entirely.
How do you train a model to enable classifier-free guidance?
During training, randomly drop the conditioning input with probability puncond≈0.1–0.2, replacing it with a null token, so the model learns both conditional and unconditional distributions.
What is the "conditioning direction" in CFG?
Δϵ=ϵθ(xt,t,c)−ϵθ(xt,t,∅), the difference between conditional and unconditional predictions, representing the influence of the conditioning signal.
Why does CFG require two model evaluations per sampling step?
Because you need separate predictions—ϵθ(xt,t,c) and ϵθ(xt,t,∅)—to compute their difference and scale it by w.
What happens if you use w<1 in CFG?
You interpolate toward the unconditional distribution, reducing the effect of conditioning below the trained level, which is usually not desired.
What is the tradeoff of using high guidance scales (w≫1)?
High w improves prompt fidelity and specificity but reduces sample diversity, can cause over-saturation or artifacts, and concentrates probability mass (mode collapse tendency).
How does CFG relate to classifier guidance?
CFG achieves the same effect as classifier guidance (amplifying ∇logp(c∣xt)) but without needing a separate noise-robust classifier, by leveraging the model's own conditional/unconditional predictions.
What is the typical range for w in Stable Diffusion text-to-image models?
w∈[7,10] for strong prompt adherence with good quality; lower values like w=1.5–3 for subtle guidance.
Chalो, ise ek simple tarike se samajhte hain. Jab hum text-to-image model se bolते hain "golden retriever dikhao", toh model ki taraf se aane wala output kai baar strong nahi hota — thoda dog-ish blob type ho jaata hai jo prompt ko theek se follow nahi karta. Classifier-free guidance ka core idea yeh hai ki hum do parallel predictions banaate hain: ek jo tumhare prompt ko jaanti hai (conditional), aur ek jo prompt ko ignore karti hai (unconditional). In dono ka difference nikaal ke humein woh "direction" milti hai jo sirf tumhara text prompt add kar raha hai — matlab "golden retriever-ness" ki direction. Fir hum us direction ko guidance scale w se multiply karke amplify kar dete hain, jaise ek "prompt volume knob" ko upar ghumana.
Ab yeh kyun kaam karta hai, iske peeche Bayes rule ki simple math hai. Conditional score ko hum unconditional score plus classifier gradient mein tod sakte hain. Purane methods mein log ek alag classifier train karte the jo isi direction ko provide karta tha, lekin woh extra kaam aur noise-robust classifier maangta tha. Classifier-free guidance mein hum ek hi model ko dono kaam — conditional aur unconditional — sikha dete hain, aur sampling ke time inke predictions ko combine kar lete hain. Formula seedha yeh ban jaata hai: ϵ~=(1−w)ϵ∅+wϵc. Jab w=1 ho toh normal conditional milta hai, aur w>1 pe hum thoda "overshoot" karte hain jisse output prompt ko aur strongly follow karta hai.
Yeh matter isliye karta hai kyunki aaj ke saare bade text-to-image systems — Stable Diffusion, DALL-E jaise — isi trick pe chalte hain. Iske bina generated images sundar toh hoti hain par tumhare prompt ko theek se match nahi karti. Bas dhyaan rakhna: w zyada badha doge toh prompt-following strong ho jaayega lekin diversity kam ho jaayegi (outputs ek jaise dikhne lagenge). Toh yeh ek trade-off hai — quality aur prompt-matching versus variety ke beech ka balance, jise hum ek single knob se control kar sakte hain, bina koi extra classifier train kiye.