Classifier-Free Guidance

Classifier-free guidance (CFG; Ho et al., 2022) makes a conditional diffusion or flow model follow its condition more strongly. The condition is typically a text prompt or a class label. One network makes two predictions at the same noisy input: a conditional one given and an unconditional one given the null condition . Training replaces by with probability , so the same network learns both. At every sampling step the two predictions are combined. In the lecture’s noise-prediction form,

where is the noise-predicting network and is the CFG scale. For a flow model the same rule acts on velocities,

and the Euler step uses in place of . The scale gives unconditional sampling, gives plain conditional sampling, and extrapolates past the conditional prediction. The lecture’s example uses . Project 3 uses for MNIST classes and for text prompts. Guidance improves prompt alignment and image quality and reduces diversity. It pushes samples toward the mode of the conditional distribution and, for large , past it. Each step costs two network evaluations.

Classifier guidance, the older method, adds a multiple of the gradient of a separately trained classifier to each update. It works poorly because noisy and blurry intermediate images are out of distribution for a classifier trained on clean images. CFG needs no classifier. By Bayes’ rule, , and the difference between the conditional and unconditional predictions is a multiple of this gradient.

Three Ways to Write the Guidance Scale

The lecture writes , where is plain conditional generation. The discussion worksheet writes , the same rule with . Project 3 Part B writes , again with , and uses . Its pseudocode calls a “classifier guidance scale”, but the update is classifier-free guidance. Project 3 Part A writes , which measures the extrapolation from the conditional prediction. There is plain conditional sampling, so , and Part A’s equals .

Direction of the Classifier Gradient

The lecture’s classifier-guidance slide labels the guidance arrow , while the same figure calls the image prediction . The index comes from DDPM notation, where is the clean image. A classifier outputs , and classifier guidance moves the image along . The two gradients differ only by , which the diffusion model already supplies.

Intuition

A model trained on a billion captioned images still follows long prompts loosely. Without guidance, “a photo of a raccoon wearing an astronaut helmet” gives a smeared mix of raccoon and metal. The conditional prediction often differs only a little from the unconditional one. Most of what the network predicts at a noisy input is generic natural-image structure, which the prompt barely changes. Guidance subtracts the unconditional prediction from the conditional one. The difference isolates the effect of the condition, and the scale amplifies it. The name refers to the classifier that the method no longer needs. Classifier guidance used a separate classifier, and CFG gets the same kind of push from the generative model itself.

The figure shows a 2D toy problem. The data lie uniformly on the three sides of a triangle, and the class “red” is the left side. At pure noise (), the exact unconditional velocity points at the center of all data. The exact conditional velocity points at the middle of the red side. The guided velocity follows the difference between them and goes further for larger . With the one-step estimate lies well outside the triangle. A larger makes samples more clearly red, but it also moves them off the data and makes them more alike.

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[scale=2.2,>=latex]
\coordinate (A) at (-1,0);
\coordinate (B) at (1,0);
\coordinate (C) at (0,1.7321);
\coordinate (X) at (0.2,-1.0);
\coordinate (M) at (0,0.5774);
\coordinate (R) at (-0.5,0.8660);
\coordinate (G2) at (-1,1.1547);
\coordinate (G3) at (-1.5,1.4434);
\draw[mid,thick] (A) -- (B) -- (C);
\draw[hot,very thick] (A) -- (C);
\node[hot,below left] at (-0.95,0.0) {red side};
\node[mid,below right] at (0.95,0.0) {data on the sides};
\filldraw (X) circle (0.9pt) node[below] {$x_0$};
\draw[->,cold,thick] (X) -- (M);
\draw[->,hot,thick] (X) -- (R);
\draw[->,very thick] (X) -- (G3);
\draw[dotted,thick] (M) -- (G3);
\filldraw[cold] (M) circle (0.9pt) node[right] {$s = 0$};
\filldraw[hot] (R) circle (0.9pt) node[above right] {$s = 1$};
\filldraw (G2) circle (0.9pt) node[above right] {$s = 2$};
\filldraw (G3) circle (0.9pt) node[left] {$s = 3$};
\draw[->,cold,thick] (1.15,1.55) -- (1.4,1.55) node[right,black] {$v(x_0, 0, \varnothing)$, unconditional};
\draw[->,hot,thick] (1.15,1.30) -- (1.4,1.30) node[right,black] {$v(x_0, 0, \text{red})$, conditional};
\draw[->,very thick] (1.15,1.05) -- (1.4,1.05) node[right,black] {$\tilde{v}$ with $s = 3$, guided};
\draw[dotted,thick] (1.15,0.80) -- (1.4,0.80) node[right,black] {one-step estimates $\hat{x}_1$ for $s = 0, 1, 2, 3$};
\end{tikzpicture}
\end{document}

Properties

(Condition Dropout) One network learns both predictions only if training sometimes hides the condition.

Project 3 Part B conditions its 6.4. U-Net on a one-hot class vector and sets that vector to zero with probability . The pretrained text-to-image model in Part A uses the embedding of the empty prompt "" as . Without dropout the network never sees during training, and its unconditional output is meaningless. At sampling time both predictions must use the same and . The two forward passes can run as one batch of size two, but guidance still doubles the compute per step.

(Implicit Classifier) The gap between the conditional and unconditional predictions is a scaled gradient of , so the model contains a classifier without training one.

Bayes’ rule gives , and the last term does not depend on . For the linear flow path, the exact noise prediction equals times the score , and the exact velocity equals . Hence

In DDPM notation the factor in the first formula is . Adding times the gap gives the prediction for the unnormalized density . For the power favors images that the implicit classifier rates as most clearly class . Guided sampling behaves only approximately like sampling from this sharpened density, because the sharpened densities at different do not fit together into one exact path.

(Diversity Trade-Off) A larger scale gives samples that match the condition better and look more alike.

The lecture’s corgi example shows varied and partly off-topic dogs at and centered, typical corgis at . A 2D toy example by Karras et al. (2024) shows the same pattern on a tree-shaped distribution. Without guidance some samples fall into the gaps between branches. With CFG the samples stay on the branches but cover only part of the tree. Project 3 Part A reports better prompt alignment and image quality at the expense of diversity. A very large pushes samples past the conditional mode and off the data, as the triangle example shows.

(Same Rule in Any Parameterization) Guidance on noise, velocity or clean-image predictions produces the same update.

The guided prediction has weights that sum to one. At fixed , the noise, velocity and clean-image predictions are affine functions of each other with coefficients that do not depend on . Affine maps preserve such combinations. For example , so

The lecture’s noise form and Project 3’s velocity form therefore define the same sampler. The guided one-step estimate is , the same extrapolation applied to images.

(Why Classifier Guidance Fails) A classifier trained on clean photos gives unreliable gradients on noisy or blurry inputs.

Classifier guidance applies a pretrained classifier, for example an ImageNet classifier, to the noisy image or to the one-step estimate . Early in sampling, is mostly noise and is a blurry average image. Neither looks like the classifier’s training data, so its class scores and their gradients carry little meaning there. The gradient can raise the class score through small pixel changes that do not make the image look more like the class. Training the classifier on noisy images at every noise level avoids part of the problem, but it requires a second large network. CFG needs only the generative model.

Examples

(Easy) Converting Guidance Scales Between Project 3 Parts

At one sampling step for a toy two-pixel image, the network returns the conditional velocity and the unconditional velocity . Part A combines them as with . Part B combines them as with .

(Medium) Guidance on a Triangle

Data points lie uniformly on the three sides of the triangle with vertices , and . The class “red” is the left side, from to , and the three classes are equally likely. A network has learned the exact conditional and unconditional flows. Sampling starts at the noise sample .

See Also