Skip to content

6.7 Guidance and conditioning

ready≈ 11.5 hoursNeeds: 6.6Unlocks: 6.8
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Guidance slider)
  4. 4 Do
  5. 5 Check

Conditioning. To generate a chosen digit, or an image matching a caption, give the network the condition y as one more input and train exactly as in 6.6, with each data point’s own label. The learned v(x, t, y) is then the marginal velocity of the data with that label, p(x | y) (Holderrieth and Erives 2025, the section on conditional generation).

Why plain conditioning is not enough. In practice samples of p(x | y) can look only loosely like their class. The fix is to exaggerate the condition. Classifier guidance did this with a separately trained classifier on noisy inputs, adding w ∇log pt(y | x) to the score (Dhariwal and Nichol 2021, section 4).

Classifier-free guidance. Ho and Salimans removed the classifier (2022, sections 2–3). Replace the label with a null token ∅ about 10% of the time during training, so one network also learns the unconditional field v(x, t, ∅). At sampling time use v(x, t, ∅) + w (v(x, t, y) − v(x, t, ∅)). By Bayes’ rule the difference of the two is, up to a factor, the gradient of log pt(y | x): the classifier was inside the network all along. w = 0 ignores the label and w = 1 is plain conditional sampling. With w > 1, samples become more recognisably their class and less varied, and they are no longer distributed as p(x | y). Their paper writes the same rule with w + 1 in place of w.

The cost. Each sampling step now calls the network twice, once with the label and once with ∅.

Guidance follows the score of p(x) p(y | x)^wstep 1 of 4
  1. ∇log⁡pt(x∣y)=∇log⁡pt(x)+∇log⁡pt(y∣x)\nabla \log p_t(x \mid y) = \nabla \log p_t(x) + \nabla \log p_t(y \mid x)

Guidance slider

Fidelity vs diversity

Static view on small screens. Open this page on a laptop to interact.

generate class
ABC
filled: 300 samples with guidance w = 3; hollow: the same noise at w = 1. Dashed: 1 and 2 standard deviations of each class
∇log pt(A | x) at time t = 0.50: the push guidance adds, proportional to v(x, t, A) − v(x, t, ∅). Circles: one standard deviation of each noised class
mean moved 0.49 from class A's centre (at w = 1: 0.05) spread 0.68 × the class's own (at w = 1: 0.93)

The guided velocity is v(x, t, ∅) + w (v(x, t, y) − v(x, t, ∅)). At w = 0 the label is ignored and samples land on all three classes; at w = 1 they are exact samples of the class. Above 1 the extra term, an implicit classifier's gradient, keeps pushing points away from wherever the other classes are likely, so the samples shrink and slide away from the neighbours: more unmistakably class A, less varied, and no longer distributed like the class. Text-to-image models are commonly sampled with w somewhere between about 3 and 8, trading diversity for fidelity in just this way.

Two square plots of a 2D mixture of three classes, A, B and C, each one Gaussian blob. Buttons choose the class to generate, jump to w = 0 or w = 1, and draw new noise; a slider sets the guidance w from 0 to 8. The left plot shows 300 samples made by following the exact guided velocity, over the same noise sampled at w = 1, with dashed circles for each class. The right plot draws the direction of the implicit classifier’s gradient at a time t set by a slider. The readout gives how far the samples’ mean has moved from the class centre and their spread relative to the class’s own. Keyboard: Tab to the buttons and sliders.

Guidance and conditioning: edit exercises/stubs/m6_7_guidance.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.7
Hints · implicit_classifier_grad 0/3 revealed
Hints · cond_fm_loss 0/3 revealed
Hints · sample_cfg 0/3 revealed

Then run the lab. Open notebooks/6.7-guidance.ipynb (a GPU helps; a CPU finishes in about 20 minutes) and paste in your 6.7 file. It checks your network against the exact guided flow in 2D, then trains a label-conditioned U-Net on MNIST and generates every digit at w = 0, 1 and 3, measuring how often the samples are the requested digit and how varied they are.

1. Why are labels replaced by a null token for some training examples?
2. What do w = 0 and w = 1 give?
3. You raise w from 1 to 6. What happens to the samples of a class?
4. Where is the "classifier" in classifier-free guidance?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.