Course contentsShow
← Machine Learning and Deep Learning
Lesson 1587 of 3,53834. Diffusion ModelsFree lesson

Classifier-Free Guidance: Sampling

Combining conditional and unconditional predictions during sampling using the guidance scale parameter.

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:

  • ε_cond is the noise prediction with your condition (e.g., "a cat")
  • ε_uncond is the noise prediction without any condition
  • w is 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:

  1. Run the model twice: once with condition, once without
  2. Apply the formula above to get ε_guided
  3. Use ε_guided for 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.