Search

Search papers and lessons

Classifier-Free Diffusion Guidance

Jonathan Ho, Tim Salimans · 2022

Ho and Salimans show that diffusion models can be steered toward a condition by mixing their conditional and unconditional predictions, with no separate classifier to train.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Generative models
  • Architectures
  • Linear algebra
  • Neural network basics
  • Sign in to see your progress

TL;DR

Diffusion models can be steered toward a class label or a text prompt to trade sample variety for sample fidelity. Earlier methods needed a separate classifier trained on noisy images to do this. Ho and Salimans show that the same effect comes from one network trained with random conditioning dropout, so it learns both the conditional and unconditional cases, then extrapolating its conditional prediction away from its unconditional one at sampling time.

Where you'll see it: almost every text-to-image system, where the "guidance scale" slider is this paper's weight ww.

The problem

Diffusion models generate good samples but, left alone, they spread across the whole data distribution. The Inception score rewards samples that a classifier labels confidently, so a model that covers all modes will score poorly on it even when its images look reasonable.

Practitioners wanted the sampling knob that GANs had: truncation, which trades diversity for fidelity. Shrinking the noise in a diffusion sampler does not work, because it produces blurry, low-quality images. The working solution before this paper was classifier guidance, which pushes samples uphill on the log-likelihood of an image classifier. That classifier had to be trained separately, on noisy images, which made the training pipeline heavier and tied sampling to gradients of a model that was not the generator.

Key idea

Classifier guidance adds a classifier gradient to the score. The paper observes that a classifier can be implied by the generative model itself. By Bayes' rule, the ratio of the conditional score to the unconditional score is the gradient of an implicit classifier. So if a single network can predict both scores, the guidance direction is available without any extra model.

The method is concrete. Train one network with the condition c\mathbf{c} replaced by a null token a fraction puncondp_{\text{uncond}} of the time. At sampling time, form the extrapolated prediction

ϵ~θ(zλ,c)=(1+w) ϵθ(zλ,c)−w ϵθ(zλ),\tilde{\boldsymbol{\epsilon}}_\theta(\mathbf{z}_\lambda, \mathbf{c}) = (1 + w)\, \boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda, \mathbf{c}) - w\, \boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda),

and sample with it in place of the ordinary prediction. Larger ww pushes the sample further from the unconditional prediction and closer to the condition. That is the whole method. The rest of the paper is about showing that the trade-off it produces is as good as classifier guidance.

The method

Background

A diffusion model corrupts data x\mathbf{x} with Gaussian noise at a log signal-to-noise ratio λ\lambda:

zλ=αλx+σλϵ,ϵ∼N(0,I).\mathbf{z}_\lambda = \alpha_\lambda \mathbf{x} + \sigma_\lambda \boldsymbol{\epsilon}, \qquad \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}).

Here αλ\alpha_\lambda and σλ\sigma_\lambda are the signal and noise scales implied by λ\lambda, and ϵθ\boldsymbol{\epsilon}_\theta is a network that predicts the added noise. Training minimises the denoising loss from DDPM, which is a weighted form of denoising score matching. The network's noise prediction is proportional to the score ∇zlog⁡p(zλ)\nabla_{\mathbf{z}} \log p(\mathbf{z}_\lambda) scaled by −σλ-\sigma_\lambda, so the noise prediction and the score carry the same information. Sampling follows the score, which is why modifying the noise prediction modifies the sampling distribution.

Classifier guidance, in one equation

Dhariwal and Nichol replaced the score by the score plus the gradient of an image classifier's log-probability of the condition c\mathbf{c}, scaled by ww. The sampled distribution then becomes approximately

p~θ(zλ∣c)∝pθ(zλ∣c) pθ(c∣zλ)w.\tilde{p}_\theta(\mathbf{z}_\lambda \mid \mathbf{c}) \propto p_\theta(\mathbf{z}_\lambda \mid \mathbf{c})\, p_\theta(\mathbf{c} \mid \mathbf{z}_\lambda)^{w}.

The factor pθ(c∣zλ)wp_\theta(\mathbf{c} \mid \mathbf{z}_\lambda)^w up-weights samples that the classifier recognises. That raises the Inception score and lowers diversity, which is the truncation-like behaviour the authors wanted. The drawback is the separately trained classifier, which has to work on noisy inputs.

Classifier-free guidance

Apply Bayes' rule to the conditional and unconditional densities. The implicit classifier is

pi(c∣zλ)∝p(zλ∣c)p(zλ).p^i(\mathbf{c} \mid \mathbf{z}_\lambda) \propto \frac{p(\mathbf{z}_\lambda \mid \mathbf{c})}{p(\mathbf{z}_\lambda)}.

Its log-gradient is a difference of two scores. Substituting exact scores into the classifier-guidance formula and rearranging gives the combination (1+w)ϵθ(zλ,c)−wϵθ(zλ)(1 + w)\boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda, \mathbf{c}) - w\boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda) shown above. The authors are careful about the status of this step. Because the networks are unconstrained, the combined field is not guaranteed to be the gradient of any scalar potential, so it is not exactly a classifier-guided score. It is inspired by one, and the paper's evidence is empirical.

Training is Algorithm 1 in the paper. For each example, draw (x,c)(\mathbf{x}, \mathbf{c}), replace c\mathbf{c} by the null token with probability puncondp_{\text{uncond}}, draw λ\lambda and ϵ\boldsymbol{\epsilon}, form zλ\mathbf{z}_\lambda, and take a gradient step on ∥ϵθ(zλ,c)−ϵ∥2\|\boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda, \mathbf{c}) - \boldsymbol{\epsilon}\|^2. Sampling is Algorithm 2. At each step compute the two predictions, combine them with ww, and pass the result to any diffusion sampler, such as ancestral sampling or DDIM. The cost is two network evaluations per step, one conditional and one unconditional.

Results

The experiments use class-conditional ImageNet at 64 by 64 and 128 by 128, with the same architecture and hyperparameters as the guided diffusion models of Dhariwal and Nichol. The paper states plainly that these experiments are a proof of concept, not a push for state of the art, though its 128 by 128 results are reported as state of the art at the time.

Guidance strength. Sweeping ww from 0 to 4 traces a clear frontier between FID and Inception score: Inception score rises steadily with ww, while FID is best at small ww and then worsens. At 64 by 64 with puncond=0.1p_{\text{uncond}} = 0.1, the best FID is 1.55 at w=0.1w = 0.1, compared with 2.07 for ADM and 1.48 for the cascaded model CDM (the latter is best). At w=4.0w = 4.0 the Inception score reaches 260.2 with FID 26.22, which is the high-fidelity end of the curve.

Sampling steps. At 128 by 128 with 256 sampling steps and w=0.3w = 0.3, the paper reports FID 2.43 and Inception score 158.47. The FID is better than the classifier-guided ADM-G at 2.97. The paper notes that 256 steps match ADM-G's step count, but each classifier-free step costs two network evaluations, so the fair speed comparison is its 128-step setting, which underperforms in FID.

Unconditional probability. Training with puncond=0.5p_{\text{uncond}} = 0.5 is consistently worse across the frontier than 0.1 or 0.2, which perform about the same. The authors read this as a sign that only a small fraction of model capacity needs to go to unconditional generation.

Figures show the qualitative effect. Stronger guidance produces more saturated colours and less variety, which is the expected truncation-like behaviour.

Why it mattered

The paper turned guidance from a classifier-dependent trick into a property of the generative model. Once the conditioning can be dropped during training, any conditional diffusion model gets a sampling-time quality knob for free. Later text-to-image systems use this exact combination, with the condition being a text embedding and ww exposed to users as a guidance scale.

The method also made the sampling trade-off explicit. Because the update is a linear extrapolation, a larger ww can push samples outside the region of likely conditional data. Practitioners later found saturated colours and reduced diversity at high guidance, which the paper already observes. The paper itself flags the loss of diversity as a concern for deployed models.

Reading guide

Read the paper in this order, and use these prerequisites for each part.

  1. Introduction and Section 2 (Background). Needs Gaussian (Normal) Distribution for the noising process and DDPM for the training loss and the noise-prediction parameterisation.
  2. Section 3.1 (Classifier guidance). Needs Score Function for why the noise prediction is a scaled score, and Score Matching for the link between denoising and score estimation. The tilted distribution p(z)p(c∣z)wp(\mathbf{z}) p(\mathbf{c} \mid \mathbf{z})^w is a product of densities, so it helps to be comfortable with Bayes' Rule.
  3. Section 3.2 (Classifier-free guidance). Needs Classifier-Free Guidance for the combined prediction, and the derivation step uses the implicit-classifier identity from Bayes' rule. Algorithms 1 and 2 are the practical core.
  4. Section 4 (Experiments). Needs Diffusion Model for the sampling loop and the notion of sampling steps, and Variational Lower Bound (ELBO) if you want the likelihood background behind the training objective. The FID and Inception score definitions are standard and need no separate lesson here.
  5. Section 5 (Discussion). Revisits the non-conservative-field point from Section 3.2 and the cost of two forward passes per step. Langevin Dynamics (also mentioned at the end of Section 2) gives the intuition that sampling follows a score field, and Denoising Diffusion Implicit Models (DDIM) covers the deterministic sampler that Algorithm 2 says can replace the ancestral step.

Check yourself

QuestionRecall: what is the combined prediction used at sampling time, and what does each term stand for?Show answer

The combined prediction is ϵ~θ(zλ,c)=(1+w)ϵθ(zλ,c)−wϵθ(zλ)\tilde{\boldsymbol{\epsilon}}_\theta(\mathbf{z}_\lambda, \mathbf{c}) = (1 + w)\boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda, \mathbf{c}) - w\boldsymbol{\epsilon}_\theta(\mathbf{z}_\lambda). The first term is the noise prediction given the condition c\mathbf{c}. The second is the noise prediction with the condition replaced by the null token, which is the unconditional prediction. The guidance weight ww controls how far the result moves away from the unconditional prediction, toward the conditional one.

QuestionComputation: suppose at some step the conditional prediction is 0.5 and the unconditional prediction is 0.2 for one coordinate, and w=3w = 3. What is the guided value, and check it another way?Show answer

Direct: (1+3)(0.5)−3(0.2)=2.0−0.6=1.4(1 + 3)(0.5) - 3(0.2) = 2.0 - 0.6 = 1.4. Rewriting the formula as ϵu+(1+w)(ϵc−ϵu)\boldsymbol{\epsilon}_u + (1 + w)(\boldsymbol{\epsilon}_c - \boldsymbol{\epsilon}_u) gives 0.2+4⋅0.3=1.40.2 + 4 \cdot 0.3 = 1.4, which agrees. The guided value lies beyond the conditional prediction, because the extrapolation keeps going past it in the same direction.

QuestionWhy: the paper says classifier-free guidance is only inspired by a classifier and is not exactly a classifier-guided score. Why can't the combined field be an exact classifier gradient?Show answer

A classifier-guided score is the score of a tilted density, which requires the guidance direction to be the gradient of a scalar function, the classifier log-probability. The combined field is built from two unconstrained neural network outputs, and such outputs need not be conservative. A vector field has a scalar potential only when its curl vanishes, and nothing in the training forces that. So there may be no classifier whose gradient equals the combined field, even though the field has the same role in sampling.

QuestionApplied: you want to add guidance to an existing conditional diffusion model without retraining it from scratch. What is the minimal change, and what does it cost at sampling time?Show answer

Fine-tune the model so that it also learns the unconditional case, by dropping the condition with some probability during continued training. This needs no separate classifier. At sampling time, run the network twice per step, once with the condition and once with the null token, and combine them with ww. The sampling cost roughly doubles per step, which is the main drawback the paper names. You would also need to tune ww, because too large a value reduces diversity and produces saturated samples.

Sources

Read the full paper

Jonathan Ho, Tim Salimans, 2022. arXiv:2207.12598