Exercises — Text-to-image conditioning (CLIP)
Before we start, one symbol reminder so nothing is used before it is named:
Here (multiply matching entries, add them up) and (the arrow's length). We use cosine and not raw dot product because we only care about which way an embedding points (its meaning), not how long it is — see contrastive learning.

Level 1 — Recognition
Exercise 1.1
Two unit-length embeddings point in exactly the same direction. What is their cosine similarity ? What if they are perpendicular? What if they point in opposite directions?
Recall Solution
Cosine similarity is where is the angle between the arrows.
- Same direction: , so .
- Perpendicular: , so .
- Opposite: , so .
This is exactly why : = "same meaning", = "unrelated", = "opposite meaning".
Exercise 1.2
In the CLIP similarity matrix for a batch, which entries are the positives (should be large) and which are the negatives (should be small)?
Recall Solution
The diagonal entries are positives — image paired with its own caption . Every off-diagonal entry with is a negative — image against someone else's caption. Training pushes the diagonal up and everything off-diagonal down.
Level 2 — Application
Exercise 2.1
Let and . Compute the cosine similarity .
Recall Solution
Dot product: . Lengths: and likewise . The arrows nearly overlap (angle ), so is close to .
Exercise 2.2
A batch of 3 has diagonal similarities and, for image 1, the row . With temperature , compute — the softmax probability that image 1 matches text 1.
Recall Solution
Divide by : logits . Exponentiate: , , . The small made the correct match dominate — the softmax is very confident.
Exercise 2.3
Same row but now . Recompute and comment.
Recall Solution
Logits . Exponentials: , , . With a large temperature the distribution is much flatter — only confidence even though is the biggest. This is why CLIP uses a small : it sharpens the softmax so correct pairs are strongly rewarded.
Level 3 — Analysis
Exercise 3.1
Show that as (and assuming is strictly the largest entry in row 1), . Explain in words what this means for training.
Recall Solution
Write (dividing top and bottom by ). For the correct term : the exponent is , giving . For every wrong : , so as , hence . Therefore the denominator and . Meaning: a tiny temperature turns the softmax into an (almost) hard argmax. Great for confident classification, but too small a makes gradients vanish for near-ties — the model stops learning fine distinctions. That is why is learned, not fixed at .
Exercise 3.2
The CLIP loss is symmetric: . Give a concrete failure that could occur if you dropped one direction and only trained .
Recall Solution
only normalises across columns for a fixed image — "given this image, pick the right caption". Nothing in it forces "given this caption, pick the right image". The text encoder could collapse many captions onto nearly the same vector: as long as each image still finds its caption slightly more than the others in its own row, can be low. The reverse loss normalises across rows for a fixed text, punishing exactly that collapse. Training both directions makes both encoders learn discriminative representations.
Exercise 3.3
In zero-shot classification (Example 3 of the parent), why does the prompt template "a photo of a dog" typically beat the bare word "dog"?
Recall Solution
CLIP was trained on real internet captions, which are almost always full phrases ("a photo of ...", "an image of ..."). The text encoder therefore lives in a region of embedding space shaped by such phrasing. Feeding a bare token "dog" lands in a slightly off-distribution spot, so its embedding is a poorer probe. Matching the training distribution with a template moves the text embedding to where the image embeddings expect their matches, raising the correct-class similarity. This is a form of transfer prompt-engineering.
Level 4 — Synthesis
Exercise 4.1
Classifier-free guidance produces , where and . Show algebraically that this equals , and interpret the special cases and .
Recall Solution
Expand: . ✓
- : — pure unconditional generation; the prompt is ignored, output is random-but-plausible images.
- : — the plain conditional prediction, no amplification. Faithful but low-diversity clamping only begins for . See classifier-free guidance for the full picture.
Exercise 4.2
Suppose at some pixel and . Compute the guided prediction for guidance scales .
Recall Solution
Use with .
- : .
- : .
- : . Notice the guided value shoots far past the conditional : guidance extrapolates along the prompt direction, which sharpens prompt-fidelity but, if is huge, over-saturates the image. This is the geometric picture in the figure below.

Exercise 4.3
The guidance term is claimed to approximate (the classifier gradient). Using , explain which learned network plays the role of each term.
Recall Solution
A diffusion model's noise prediction is proportional to a score (negative gradient of a log-density): and .
- learns the marginal score (images in general).
- learns the conditional score (images given the prompt). Subtracting: by the Bayes identity. So their difference is (up to sign/scale) the implicit classifier gradient — with no separate classifier ever trained. The CLIP text embedding is what supplies .
Level 5 — Mastery
Exercise 5.1
You are given three unit embeddings in : image , and two text candidates and . With , compute the zero-shot probability the image is class . Then state what happens to that probability as .
Recall Solution
Since all are unit vectors, cosine similarity is just the dot product. , . Logits: , . Almost certainly class — its arrow sits much closer to the image arrow ( vs. ). As : both logits , so . Infinite temperature erases all information — the classifier becomes a coin flip. (This is the mirror of Exercise 3.1.)

Exercise 5.2 (Design)
Combine everything: describe end-to-end how a text prompt becomes a guided denoising step, naming every module and where CLIP enters.
Recall Solution
- Encode text. The prompt string goes through CLIP's text encoder (a transformer using attention) to produce .
- Condition the U-Net. At each diffusion timestep , the noisy latent and the timestep are fed to the noise network. Cross-attention layers attend from image tokens to the CLIP text tokens — this is where the conditioning physically happens.
- Two forward passes. Run the network once with () and once with the null embedding ().
- Guide. Form with e.g. (Ex 4.1–4.3).
- Denoise. Plug into the reverse step .
- Repeat for all down to , then decode the latent to an image. CLIP appears exactly once — at step 1 — but its embedding is what steers every subsequent step. This ties together diffusion, guidance and multimodal learning.