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

Classifier Guidance: Implementation

Computing classifier gradients during sampling and adjusting noise predictions for guided generation.

Classifier Guidance: Implementation

What you'll learn: How to actually compute classifier gradients during sampling and adjust noise predictions to steer diffusion model generation toward desired classes.

The Core Idea

You've learned the theory of classifier guidance—now let's implement it. During each reverse diffusion step, you need to:

  1. Compute the classifier's gradient with respect to the noisy image
  2. Scale this gradient by a guidance strength parameter
  3. Adjust the noise prediction by subtracting this scaled gradient

Think of it like a GPS correction: at each step of your journey (denoising), you check how "off course" you are from your destination (target class) and nudge your next step in the right direction.

Implementation Steps

Computing the Gradient

At timestep t, you have a noisy image x_t. To apply classifier guidance:

# Enable gradient computation for the noisy image
x_t.requires_grad = True

# Get classifier prediction on noisy image
logits = classifier(x_t, t)  # Classifier is noise-aware
class_prob = logits[target_class]

# Compute gradient of log probability w.r.t. noisy image
gradient = torch.autograd.grad(class_prob.log(), x_t)[0]

Adjusting the Noise Prediction

The guided noise prediction combines the original unconditional prediction with the classifier gradient:

# Get unconditional noise prediction
noise_pred = unet(x_t, t)

# Apply guidance
guidance_scale = 1.0  # Hyperparameter controlling strength
guided_noise = noise_pred - guidance_scale * sigma_t * gradient

The sigma_t term (variance schedule value) ensures proper scaling across timesteps.

Key Takeaway: Classifier guidance implementation requires computing gradients from a noise-aware classifier at each sampling step and using them to modify the diffusion model's noise predictions, steering generation toward your target class.