Visual walkthrough — Text-to-image conditioning (CLIP)
Everything below assumes only: you can measure the angle between two arrows, and you know that a bigger number beats a smaller number. That's it.
Step 1 — Turn a picture and a sentence into arrows
WHAT. A neural network reads a photo and spits out a list of numbers. A different network reads a sentence and spits out its own list of numbers. A list of numbers is an arrow in -dimensional space (a vector). We call the image arrow and the text arrow .
WHY. We cannot compare a JPEG to English directly — they live in different worlds. But if both become arrows in the same space, we can ask a single simple question: do these two arrows point the same way? That shared space is the whole trick.
PICTURE. Below, a dog photo becomes the cyan arrow, the words "a dog" become the amber arrow. Right now they point in random directions — training hasn't happened yet.
Now let us name the two machines that do this arrow-making. "The image network" is a mouthful, so we give it a short name: . It is just a label for the whole image-reading network — feed it an image , and out comes the arrow. We write that as , read aloud as " of equals ", i.e. "run image through the image network and you get the image arrow." The parentheses just mean "here is the input I'm feeding in." Likewise is the label for the whole sentence-reading network, and means "run sentence through the text network and you get the text arrow." The subscript always means image, always means text — that's all and are: shorthand names for the two networks from the picture above.
Here = "where the image landed", and = "where the words landed".
Step 2 — Measure "same direction" with the cosine
WHAT. To score how aligned two arrows are, we use the cosine of the angle between them. If the angle is they point identically → cosine . If they are perpendicular () → cosine . If they point oppositely () → cosine .
WHY this tool and not another? We could measure the straight-line distance between the arrow tips, but that gets fooled by length. A long arrow and a short arrow can point the same way yet be far apart. We only care about direction (the meaning), never how long the network happened to make the arrow. The cosine throws length away and keeps only direction — exactly what we want.
The cosine is computed from the dot product (multiply matching entries, add them up) divided by the two lengths:
- — the similarity score between image and text . Big = they agree.
- — arrow of the -th image in the batch; the superscript is just "which one".
- — the length of an arrow (its magnitude).
PICTURE. Same arrow pair, now with the angle marked and the cosine dial from to .
Step 3 — Lay every pair on a grid: the similarity matrix
WHAT. Take a batch of pairs. Score every image against every text. That fills an grid called the similarity matrix . Formally,
- — the whole grid of scores; rows (images), columns (texts).
- — the entry in row , column : the similarity of image with text .
The diagonal () holds the true matches; everything off-diagonal () holds mismatches.
WHY. To teach "these two belong together" you must also show counter-examples of things that don't. The off-diagonal cells are the free negatives — image 1 vs text 2, image 1 vs text 3, and so on. See 4.3.8-Contrastive-learning for why negatives are the engine of this whole idea.
PICTURE. A grid. Amber diagonal = the pairs we want high. Faded cyan off-diagonal = the pairs we want low.
Step 4 — Sharpen the scores with temperature
WHAT. Divide every score by a small positive number (tau): .
WHY. Cosine scores are squashed into — a winning and a losing are barely different numbers. In the next step we exponentiate them, and small gaps make weak decisions. Dividing by a small stretches the gaps: vs — the winner-to-loser distance grows from to about , so the winner now towers over the loser. is a dial for confidence: small = sharp/decisive, large = soft/hesitant. CLIP even learns the best .
- — the stretched score, feeding the exponential next.
- — temperature; smaller = sharper. Named after physics: cold systems are decisive.
PICTURE. One row of scores before and after dividing by — the tall winner emerges.
Step 5 — Turn a row of scores into probabilities (softmax)
WHAT. For image , we want a probability for each candidate text : "how likely is text the true caption?" The recipe: exponentiate every stretched score, then divide by their total. This recipe has a standard name — it is called the softmax function, and any time you see "softmax" from now on it means exactly this "exponentiate then divide by the sum" operation.
WHY exponentiate? Two reasons in one move. (1) is always positive, so we get valid probabilities (no negatives). (2) grows fast, so a slightly larger logit becomes a much larger share — the winner-takes-most behaviour we sharpened for in Step 4. Dividing by the row total forces the probabilities to sum to .
- — probability the model assigns text to image ; the bar means "given image ".
- — the exponential, our positive-and-amplifying function.
- — add over all texts in the row (index sweeps ); this is the normaliser.
- The whole right-hand side, taken together, is the softmax of row .
PICTURE. The row of logits becomes a bar chart of probabilities that sum to a full bar of height .
Step 6 — Score the guess: cross-entropy loss
WHAT. We know the right answer for image : it is text (the diagonal). We want to be . The loss for image is . Average over the batch to get one number.
WHY the ? A perfect guess gives — zero penalty, perfect. A terrible guess gives — huge penalty. So is a pain-meter: no pain when right, unbounded pain when confidently wrong. This is exactly cross-entropy against a one-hot target (all the truth sits on cell ).
- — the image→text loss: each image must find its own caption.
- — the diagonal score, the right answer we want on top.
- — average the pain over all images.
Both directions. By symmetry we also make each text find its own image, giving (same formula but summing down columns). The final CLIP loss averages the two:
Training both directions stops the model from lazily solving only one side. Under the hood this is transfer learning fuel: the encoders end up general enough to reuse anywhere.
PICTURE. The pain-meter curve : flat at zero when right, shooting to infinity when wrong.
Step 7 — Edge cases: what happens at the extremes
WHAT & WHY. A derivation is only trustworthy if it survives its boundaries. Walk each one:
- Perfect batch — diagonal cosines , off-diagonal . After the diagonal explodes upward, , loss . Nothing left to learn — correct.
- Random batch (start of training) — every score , so every probability . Then loss . For that is . This is the baseline you should see on step 0.
- Duplicate captions — if texts and are identical, the model cannot separate them and the loss can never reach . Not a bug; it's the data being ambiguous.
- Batch size — one pair, no negatives, denominator is a single term, so always and loss regardless. The model learns nothing. This is why CLIP needs large batches — negatives are the teacher.
PICTURE. Three mini-grids: perfect (bright diagonal), random (uniform grey), and (a single lit cell that teaches nothing).
Step 8 — From trained CLIP to steering an image generator
First, three brand-new symbols. In this step we leave CLIP and hand its text arrow to a diffusion generator, which introduces things we have not met yet.
What is ? The diffusion network, like any neural network, is a giant bag of adjustable numbers (its weights) that training slowly tunes. We bundle all of those tunable numbers into one symbol, (theta), and read it as "the model's parameters." Writing (epsilon-sub-theta) just means "the noise-prediction produced by the network whose weights are " — the subscript reminds us this output depends on the trained weights, not on some fixed formula. So is "run the trained network on the noisy image at step , told about condition , and read off its guess for the noise."
WHAT. Once CLIP's arrows are aligned, feed the text arrow into the diffusion model as the condition. At each denoising step the model predicts the noise in two ways — with the prompt and without — and combines them:
- — the noisy latent (half-cleaned image) at step .
- — the diffusion time step (which round of denoising we are on).
- — all the trained weights of the diffusion network.
- — the diffusion network's predicted noise inside .
- — the CLIP text arrow; — empty prompt.
- — guidance scale; how hard we lean toward the prompt.
- The bracket is a direction: "conditional minus unconditional" = the way the words tug the image.
WHY. The difference isolates the prompt's pull; scaling it by amplifies that pull. This is exactly classifier-free guidance riding on top of the diffusion denoiser, using attention to inject .
PICTURE. A vector diagram: unconditional noise as one arrow, the prompt-direction added and stretched by to reach the guided noise .
The one-picture summary
The figure below is the whole page as a labelled conveyor belt: each stage is a box, and above every arrow between boxes is written the exact quantity that flows out of it — so you can watch the data change form step by step rather than reading a bare list of names. Narrate it out loud from the left: two inputs (image , text ) enter; the two networks emit arrows (Step 1); scoring every pair with the cosine yields the grid (Steps 2–3); dividing by produces logits (Step 4); softmax turns each row into probabilities (Step 5); of the diagonal gives the loss (Step 6); and the now-aligned text arrow becomes the condition that steers the generator (Step 8). Every symbol you met is exactly one label on this belt — if you can name each arrow's cargo, you own the derivation.
Recall Feynman retelling — say it back in plain words
We hand a computer a photo and a sentence. Two networks, and , squeeze each into an arrow living in the same room. To ask "do these mean the same thing?" we measure the angle between the arrows and take its cosine — for same direction, for opposite — and we ignore how long the arrows are because length isn't meaning. In a batch of pairs we score every photo against every sentence, filling a grid whose diagonal is the true couples. We divide every score by a tiny number to stretch the gaps, then run each row through softmax so the scores become probabilities that add to one. We punish the model with of the probability it put on the correct partner: no pain when it's sure and right, infinite pain when it's sure and wrong. We do this both ways — photos finding sentences and sentences finding photos — and average. Slowly the grid turns bright-diagonal, dark-elsewhere: matching arrows now point together. Finally we hand a sentence-arrow to an image generator whose weights are called , which computes "with prompt minus without prompt" to get a direction pointing toward the words, and cranks that direction up by a knob to paint exactly what we asked for.
Recall Quick checks
What does dividing the dot product by both lengths accomplish? ::: It removes arrow length, leaving only direction — i.e. it turns the dot product into the cosine, so only meaning is scored, not magnitude. Why exponentiate the scores before normalising? ::: is always positive (valid probabilities) and grows fast (amplifies the winner), giving a decisive winner-take-most distribution. At the very start of training, what loss value should you see for batch size ? ::: About , because every probability is roughly and . In guidance, what does the bracket represent? ::: A direction vector pointing from "no prompt" toward "the prompt" — the pull the text exerts, which then amplifies. What does the subscript in stand for? ::: All the trained weights (parameters) of the diffusion network; it reminds us the noise prediction depends on those learned numbers. Why can't CLIP learn with batch size 1? ::: With only one pair there are no negatives, so the softmax denominator is a single term, forcing and loss every time — zero gradient, so the encoders never get pushed and nothing is learned.