CLIP › Worked Example: Computing the Contrastive Loss
Language AI HandbookAll chapters CLIP › Worked Example: Computing the Contrastive LossOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's trace through a minimal example to solidify the mechanics. Suppose we have a batch of image-text pairs:
- Image 1: Dog, Text: "a dog playing fetch"
- Image 2: Cat, Text: "a cat sleeping on a couch"
- Image 3: Bird, Text: "a bird in flight"
After encoding and L2 normalization, we obtain cosine similarities. For this simplified illustration, we treat these values as the scaled similarity scores (assuming a temperature of for clarity):
| Text 1 | Text 2 | Text 3 | |
|---|---|---|---|
| Image 1 | 0.9 | 0.1 | 0.2 |
| Image 2 | 0.2 | 0.85 | 0.15 |
| Image 3 | 0.1 | 0.3 | 0.88 |
The diagonal represents correct pairs. To compute the image-to-text loss for Image 1, we calculate the softmax probability of the correct match:
where:
- : the softmax probability that Text 1 is the correct match for Image 1
- : the cosine similarity score between Image 1 and Text 1 (the correct pair)
- : the cosine similarity score between Image 1 and Text 2 (an incorrect pair)
- : the cosine similarity score between Image 1 and Text 3 (an incorrect pair)
- : the exponential values , , and respectively
- : the final probability value (approximately 51.4%)
The loss contribution is .
For Image 2:
where:
- : the softmax probability that Text 2 is the correct match for Image 2
- : the cosine similarity score between Image 2 and Text 2 (the correct pair)
- : the cosine similarity score between Image 2 and Text 1 (an incorrect pair)
- : the cosine similarity score between Image 2 and Text 3 (an incorrect pair)
- : the exponential values , , and respectively
- : the final probability value (approximately 49.5%)
Loss contribution: .
For Image 3:
Loss contribution: .
Averaging the three image-to-text losses:
The text-to-image losses are computed symmetrically by treating each text as a query and computing the softmax across image similarities (summing down columns rather than across rows). The final CLIP loss averages both directions: .
Notice that with only three items in the batch, the model achieves about 51% probability on the correct pair. In a large batch with 32,768 items, achieving 51% probability on the correct pair out of thousands of candidates would represent excellent performance. As training progresses, the model should push these diagonal values higher while driving off-diagonal values lower, ultimately achieving near-perfect discrimination within each batch.

Averaging all three image-to-text losses and computing the symmetric text-to-image component gives the final batch loss. This process encourages the model to adjust embeddings so that correct pairs (diagonal values) have higher similarity than incorrect pairs (off-diagonal values), effectively learning to discriminate between matching and non-matching pairs within the batch. The gradients flow back through both encoders, updating weights to increase the separation between positive and negative pairs. Over millions of such batches, the encoders converge toward representations where visual and linguistic descriptions of the same concept occupy the same neighborhood in the shared embedding space.