ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Diffusion Models for Generative AI
Chapter 1: Generative Modeling Fundamentals
Overview of Generative Models
Motivation for Diffusion Models
The Core Idea: Noise and Denoise
Probabilistic Framework Introduction
Quiz for Chapter 1
Chapter 2: The Forward Diffusion Process
Defining the Markov Chain
Gaussian Noise Schedule
Mathematical Formulation per Step
Sampling from Intermediate Steps
Properties of the Forward Process
Practice: Simulating Forward Diffusion
Quiz for Chapter 2
Chapter 3: The Reverse Diffusion Process
The Goal: Reversing the Markov Chain
Approximating the Reverse Transition
Parameterizing the Reverse Process with Neural Networks
Predicting the Noise Component
Mathematical Formulation of the Denoising Step
Quiz for Chapter 3
Chapter 4: Model Architecture and Training
The U-Net Architecture for Noise Prediction
Integrating Timestep Information
Defining the Training Objective
Simplified Training Loss Derivation
The Training Algorithm
Hands-on Practical: Setting up the U-Net
Quiz for Chapter 4
Chapter 5: Sampling and Generation Process
Generating Data from Noise
The DDPM Sampling Algorithm
Understanding Sampling Variance
Introduction to Faster Sampling: DDIM
The DDIM Sampling Algorithm
Trade-offs Between DDPM and DDIM
Practice: Implementing Sampling Loops
Quiz for Chapter 5
Chapter 6: Conditional Generation with Diffusion Models
Motivation for Conditional Generation
Classifier Guidance
Classifier-Free Guidance (CFG)
Implementing Classifier-Free Guidance
Text Conditioning Basics
Architecture Modifications for Conditioning
Hands-on Practical: Applying Guidance
Quiz for Chapter 6

Classifier-Free Guidance (CFG)

Steering the diffusion process for conditional generation often requires training and maintaining a separate classifier model alongside the diffusion model. This approach adds complexity and potential points of failure. Furthermore, the gradients from such a classifier might not always align perfectly with the diffusion model's internal representation, sometimes leading to artifacts or suboptimal results.

Classifier-Free Guidance (CFG) offers an elegant and effective alternative that achieves conditional generation without needing an external classifier. The main idea is to train the diffusion model itself to handle both conditional and unconditional generation scenarios.

How CFG Training Works

To enable CFG, the noise prediction network, typically a U-Net denoted as ϵθ(xt,t,y)\epsilon_\theta(x_t, t, y)ϵθ​(xt​,t,y), is trained on both conditional and unconditional inputs. During training, the conditioning information yyy (like a class label or text embedding) is randomly dropped or replaced with a special "null" token ∅\emptyset∅ some percentage of the time (e.g., 10-20% of training examples).

So, the model learns to predict:

  1. The noise given the condition: ϵθ(xt,t,y)\epsilon_\theta(x_t, t, y)ϵθ​(xt​,t,y)
  2. The noise without any condition: ϵθ(xt,t,∅)\epsilon_\theta(x_t, t, \emptyset)ϵθ​(xt​,t,∅)

This joint training forces the model to understand the difference between generating data specific to condition yyy and generating data typical of the overall distribution. The same network parameters θ\thetaθ are used for both tasks. This means the U-Net architecture must be adapted to accept the conditioning information yyy as an additional input, alongside the noisy image xtx_txt​ and the timestep ttt.

CFG Sampling: Guiding the Denoising Step

During the sampling (reverse diffusion) process, we leverage the model's ability to predict both conditional and unconditional noise. At each denoising step ttt, we perform two forward passes through the U-Net using the current noisy state xtx_txt​ and timestep ttt:

  • One pass provides the conditional noise prediction: ϵθ(xt,t,y)\epsilon_\theta(x_t, t, y)ϵθ​(xt​,t,y), using the desired condition yyy.
  • Another pass provides the unconditional noise prediction: ϵθ(xt,t,∅)\epsilon_\theta(x_t, t, \emptyset)ϵθ​(xt​,t,∅), using the null token ∅\emptyset∅ in place of the condition.

The intuition is that the difference between these two predictions, ϵθ(xt,t,y)−ϵθ(xt,t,∅)\epsilon_\theta(x_t, t, y) - \epsilon_\theta(x_t, t, \emptyset)ϵθ​(xt​,t,y)−ϵθ​(xt​,t,∅), represents the direction in the noise space that moves the generation towards satisfying the condition yyy. CFG combines these predictions to create an adjusted noise estimate ϵ^θ\hat{\epsilon}_\thetaϵ^θ​ that extrapolates further in the conditional direction.

The formula for the CFG-adjusted noise prediction is:

ϵ^θ(xt,t,y,w)=ϵθ(xt,t,∅)+w(ϵθ(xt,t,y)−ϵθ(xt,t,∅))\hat{\epsilon}_\theta(x_t, t, y, w) = \epsilon_\theta(x_t, t, \emptyset) + w (\epsilon_\theta(x_t, t, y) - \epsilon_\theta(x_t, t, \emptyset))ϵ^θ​(xt​,t,y,w)=ϵθ​(xt​,t,∅)+w(ϵθ​(xt​,t,y)−ϵθ​(xt​,t,∅))

Here, www is the guidance scale.

  • If w=0w = 0w=0, the formula simplifies to ϵ^θ=ϵθ(xt,t,∅)\hat{\epsilon}_\theta = \epsilon_\theta(x_t, t, \emptyset)ϵ^θ​=ϵθ​(xt​,t,∅). This corresponds to purely unconditional generation, ignoring yyy.
  • If w=1w = 1w=1, we get ϵ^θ=ϵθ(xt,t,y)\hat{\epsilon}_\theta = \epsilon_\theta(x_t, t, y)ϵ^θ​=ϵθ​(xt​,t,y). This is equivalent to standard conditional generation using only the conditional prediction without any explicit guidance boost.
  • If w>1w > 1w>1, the process extrapolates. It starts with the unconditional prediction ϵθ(xt,t,∅)\epsilon_\theta(x_t, t, \emptyset)ϵθ​(xt​,t,∅) and moves further in the direction indicated by the condition yyy (represented by the difference vector), scaled by www. This effectively amplifies the influence of the condition yyy on the generation process.

This adjusted noise estimate ϵ^θ(xt,t,y,w)\hat{\epsilon}_\theta(x_t, t, y, w)ϵ^θ​(xt​,t,y,w) is then used within the denoising update rule (e.g., in DDPM or DDIM sampling) to calculate the estimate for the less noisy state xt−1x_{t-1}xt−1​.

The Guidance Scale (w)

The guidance scale www is a hyperparameter that allows you to control the strength of the conditioning. It acts as a knob balancing adherence to the condition against sample diversity and quality.

  • Low www (e.g., 1-3): Weak guidance. Samples generated tend to be more diverse and creative but might only loosely follow the provided condition yyy.
  • Moderate www (e.g., 5-10): Often provides a good balance. Samples usually adhere well to the condition yyy while maintaining reasonable visual quality and some degree of diversity. This range is frequently used in practice for text-to-image models.
  • High www (e.g., 15+): Strong guidance. Samples very closely follow the condition yyy. However, this can sometimes lead to reduced diversity (samples look similar), potential oversaturation of colors or features, or other artifacts as the extrapolation pushes the generation process into areas less explored during training.

Choosing the right value for www often requires some experimentation based on the specific diffusion model, the dataset it was trained on, and the desired output characteristics.

A diagram illustrating how Classifier-Free Guidance combines the unconditional noise prediction ϵθ(xt,∅)\epsilon_\theta(x_t, \emptyset)ϵθ​(xt​,∅) and the conditional prediction ϵθ(xt,y)\epsilon_\theta(x_t, y)ϵθ​(xt​,y) to produce an adjusted prediction ϵ^θ(xt,y,w)\hat{\epsilon}_\theta(x_t, y, w)ϵ^θ​(xt​,y,w) by extrapolating along the difference vector, controlled by the guidance scale www. This example shows extrapolation for w=2w=2w=2.

Advantages of CFG

Classifier-Free Guidance has become a standard technique for conditional diffusion models due to several significant advantages:

  1. No Extra Classifier: It eliminates the need to train, manage, and ensure compatibility of a separate classification model. This simplifies the overall pipeline.
  2. Simplicity: The implementation primarily involves modifying the training data (by randomly dropping conditions) and the sampling loop (by computing two forward passes through the U-Net per step and combining the results using the CFG formula).
  3. Effectiveness: CFG often yields high-quality conditional samples that strongly adhere to the provided guidance. It generally performs as well as or better than classifier guidance across various tasks, particularly in text-to-image generation.
  4. Flexibility: The guidance scale www provides a simple and intuitive way to control the trade-off between condition adherence and sample diversity at inference time, without needing to retrain the model.

By integrating the conditioning mechanism directly into the diffusion model's training objective, CFG provides a powerful and widely adopted method for controlling the generation process. We will explore how to implement this technique in practice later in this chapter.

  • Classifier-Free Diffusion Guidance, Jonathan Ho, Tim Salimans, 2021 NeurIPS 2021 Workshop on Deep Generative Models and Downstream Applications DOI: 10.48550/arXiv.2207.12598 - Introduces the concept of Classifier-Free Guidance, detailing its training methodology, sampling algorithm, and the role of the guidance scale.

© 2026 ApX Machine Learning