Classifier-Free Guidance: Sampling
What You'll Learn
You'll learn how to blend conditional and unconditional predictions during sampling using a guidance scale parameter to control generation strength.
The Core Idea
During sampling (generation), classifier-free guidance combines two model predictions: one conditional (guided by your prompt/class) and one unconditional (no guidance). You control the mix with a guidance scale parameter, typically denoted as w or s.
The sampling formula is:
ε_guided = ε_uncond + w * (ε_cond - ε_uncond)
Where:
ε_condis the noise prediction with your condition (e.g., "a cat")ε_uncondis the noise prediction without any conditionwis the guidance scale (how strongly to follow your prompt)
How It Works
Think of it like adjusting a "creativity vs obedience" dial:
- w = 0: Pure unconditional generation (ignores your prompt entirely)
- w = 1: Standard conditional generation (follows prompt normally)
- w > 1: Amplified guidance (exaggerates the prompt's influence)
When w = 7 (common in Stable Diffusion), you're saying "move 7 times farther away from the unconditional prediction toward the conditional one." This produces images that strongly match your text but may sacrifice some diversity.
Implementation During Sampling
At each denoising step, you:
- Run the model twice: once with condition, once without
- Apply the formula above to get
ε_guided - Use
ε_guidedfor the denoising update
Higher guidance scales yield more prompt-adherent but potentially less diverse samples.
Key Takeaway: Classifier-free guidance sampling blends conditional and unconditional predictions using a guidance scale parameter, letting you control how strongly the model follows your prompt—higher scales mean stronger adherence.