๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #304

Diffusion Conditioning

A plain diffusion model, as covered so far, generates unconditionally โ€” random samples from whatever distribution it learned, with no control over content. Conditioning lets you steer generation toward specific desired content, such as matching a text description.

Injecting Conditioning Into the U-Net

The most common approach for text conditioning uses cross-attention โ€” the exact mechanism from Cross-Attention โ€” inserted at multiple layers within the U-Net. The U-Net's own spatial features act as the query; the text embedding (typically produced by a separate pretrained text encoder, like CLIP's text encoder) provides the keys and values. This lets every spatial location in the image being generated selectively attend to whichever words in the text prompt are most relevant to that specific region.

Classifier-Free Guidance

A widely-used technique for strengthening how closely generation follows the conditioning: train the model to sometimes generate unconditionally (by randomly dropping the conditioning signal during a fraction of training steps), then at generation time, compute both a conditional and unconditional prediction and combine them:

\[ \hat{\boldsymbol\epsilon} = \boldsymbol\epsilon_\theta(\mathbf{x}_t, \varnothing) + w\big(\boldsymbol\epsilon_\theta(\mathbf{x}_t, c) - \boldsymbol\epsilon_\theta(\mathbf{x}_t,\varnothing)\big) \]

\(c\) is the conditioning (e.g. text embedding), \(\varnothing\) represents no conditioning, and \(w>1\) is the guidance scale. This effectively exaggerates the difference the conditioning makes, pushing generation to follow the prompt more strongly than the raw conditional prediction alone would โ€” the "guidance scale" setting many text-to-image tools expose directly to users.

Diagram

Text prompt → embedding U-Net (cross-attn at each layer) predicted noise, conditioned on the text

The text embedding is injected via cross-attention throughout the U-Net, steering every denoising step toward matching the prompt.

Code

from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")

image = pipe(
    "a serene Japanese garden in autumn",
    guidance_scale=7.5,   # the classifier-free guidance scale -- higher = follows the prompt more strongly
    num_inference_steps=50
).images[0]

Common Mistakes

  • Setting the guidance scale extremely high expecting proportionally better prompt adherence with no downside โ€” excessively high guidance scales can produce over-saturated, artifact-heavy, or visually unnatural images, not just "more accurate" ones.
  • Assuming conditioning only works via text โ€” the same cross-attention injection pattern generalizes to other conditioning signals (class labels, sketches, depth maps), directly echoing the conditional GAN idea from Conditional GAN.

Interview Relevance

Q: "How does classifier-free guidance strengthen a diffusion model's adherence to a text prompt?" By training the model to also generate unconditionally (with conditioning randomly dropped some fraction of the time), then at generation time, computing both a conditional and unconditional noise prediction and extrapolating away from the unconditional one toward the conditional one, scaled by a guidance factor \(w>1\). This exaggerates the effect of the conditioning signal, producing outputs that follow the prompt more strongly than the raw conditional prediction alone.

Practice Question

Why does the U-Net's spatial features serve as the query in text-conditioning cross-attention, rather than the text embedding?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Diffusion Conditioning โ€“ FAQs

Quick answers about learning Diffusion Conditioning in Deep Learning.

This free note from CodingNow 2.0 explains Diffusion Conditioning in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Diffusion Conditioning, is 100% free with no signup required.
With focused practice, most students grasp Diffusion Conditioning in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now