#111CLIP InfoNCE LossMedium

CLIP InfoNCE Loss

Background

The CLIP training objective is a symmetric InfoNCE loss over a batch of paired image and text embeddings. For each image, the matched text is the positive; the other N1N-1 texts in the batch are negatives. Symmetric: do it once with image as query, once with text as query, average. Foundation of every modern multimodal retrieval system.

Problem statement

Implement clip_infonce(image_embs, text_embs, temperature=0.07). Both inputs are L2-normalised, shape (N,d)(N, d). Compute the scaled similarity matrix and the symmetric cross-entropy:

logits=ZimgZtextτ,L=12(Limgtext+Ltextimg)\text{logits} = \frac{Z_{\text{img}} Z_{\text{text}}^\top}{\tau},\qquad \mathcal L = \tfrac{1}{2}\bigl(\mathcal L_{\text{img}\to\text{text}} + \mathcal L_{\text{text}\to\text{img}}\bigr)

where each direction is a softmax cross-entropy with labels = arange(N) (the diagonal).

Input

  • image_embsnp.ndarray shape (N,d)(N, d), L2-normalised.
  • text_embsnp.ndarray same shape, L2-normalised.
  • temperaturefloat > 0, softmax temperature (default 0.07, CLIP's published value).

Output

Returns a Python float >= 0.

Examples

Example 1 — identical paired embeddings → near-zero loss

Input:  image_embs == text_embs (random unit-norm)
Output: small positive value (entropy of the softmax)

Example 2 — shape mismatch raises

Input:  image (4, 8) vs text (5, 8)
Output: ValueError

Constraints

  • Image and text must have matching shape (same NN and same dd).
  • Use numerically stable log-softmax (subtract max before exp).
  • Return a plain Python float.

Notes

  • Temperature matters. Too high → soft assignment, weak gradient. Too low → degenerate hard assignment. CLIP learns τ\tau jointly; many derivatives fix it at 0.070.07 or 0.10.1.
  • Batch size. InfoNCE quality scales with the number of negatives. Production CLIP uses batches of 32K+ via gradient accumulation or distributed all-gather.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • reference: identical paired embeddings give a small loss
  • example: a single pair has zero loss
  • sample: matches the symmetric InfoNCE value on a random batch