6.5.5 · D3Research Frontiers & Practice

Worked examples — Multimodal models (vision-language)

2,152 words10 min readBack to topic

This page is the worked-example lab for the parent topic. The parent built the machinery (encoders, the attention-based transformer encoders, cosine similarity, the contrastive loss). Here we run that machinery by hand across every case it can throw at you — every sign of similarity, every degenerate input, every limit of the temperature, plus a word problem and an exam twist.

Everything is built from zero. If a symbol shows up, it was defined first.


The scenario matrix

Every question this topic asks reduces to comparing arrows with cosine similarity and squeezing those comparisons through a softmax with a temperature. Here is every case class that can appear:

Cell Case class What's special Hit by
A Positive alignment arrows point similar way, sim Ex 1
B Orthogonal arrows at right angles, sim Ex 2
C Opposite arrows point away, sim Ex 2
D Zero / degenerate vector one arrow has length — sim undefined Ex 3
E Magnitude invariance scaling an arrow must not change sim Ex 4
F Softmax + temperature (normal) full loss on a batch, Ex 5
G Limit sharpening to a hard argmax Ex 6
H Limit flattening to pure uncertainty Ex 6
I Real-world word problem zero-shot retrieval decision Ex 7
J Exam twist noisy-pair robustness / symmetric loss trap Ex 8

We now cover every cell.


Reusable tool — cosine similarity, defined from the picture

Figure — Multimodal models (vision-language)

Worked examples

Example 1 — Cell A: positive alignment

Forecast: guess before computing — both arrows lean up-and-right, close together. Do you expect sim near , near , or near ?

  1. Dot product: Why this step? The dot product is the numerator — it measures raw agreement coordinate by coordinate.
  2. Lengths: , and Why this step? We must strip out size so only direction survives.
  3. Similarity: Why this step? Dividing by the lengths converts "agreement" into a pure .

Verify: is in ✓ and close to , matching the forecast that the arrows nearly overlap. The angle is — a small gap, as drawn.


Example 2 — Cells B & C: orthogonal and opposite

Forecast: which one gives exactly , and which gives exactly ?

Part (B) orthogonal:

  1. Dot: Why? A dot product of zero is the algebraic signature of a right angle.
  2. sim Cell B done — completely unrelated concepts.

Part (C) opposite:

  1. Dot: Why? Every coordinate disagrees in sign, so the sum is fully negative.
  2. Lengths:
  3. sim Cell C done — the arrows point apart.

Verify: angles are ✓ and ✓. Both fall in . Note: negative similarity is allowed and meaningful — it says "actively push these apart."

Figure — Multimodal models (vision-language)

Example 3 — Cell D: the degenerate zero vector

Forecast: will the formula give , , or something forbidden?

  1. Dot:
  2. Length:
  3. sim undefined (division by zero). Why this matters: a zero arrow has no direction, so "which way does it point?" has no answer.

Verify: you cannot report a number. In practice code either (a) adds a tiny so and sim , or (b) rejects the batch. The lesson: always guard against zero-length embeddings — they crash cosine similarity. This is the single most common silent bug when training these models with cross-entropy-style contrastive loss.


Example 4 — Cell E: magnitude invariance

Forecast: guess whether the two similarities are equal or different.

  1. Original: from Example 1,
  2. Scaled dot:
  3. Scaled length:
  4. Scaled sim:

Verify: identical — ✓. Why this step mattered: it proves cosine ignores magnitude. This is exactly why we chose cosine (not the raw dot product): a concept's identity is its direction, and transfer to unseen data relies on that stability.


Reusable tool — softmax with temperature, defined from zero


Example 5 — Cell F: full softmax + loss, normal temperature

Forecast: with a gap of and small , do you expect near or near ?

  1. Scale by : , , Why this step? Dividing by multiplies the gaps tenfold — this is where "confidence" is dialed in.
  2. Exponentiate: , , Why? makes all terms positive and explodes the leader.
  3. Sum:
  4. Loss:

Verify: is in ✓, the three probabilities sum to ✓, and the tiny loss confirms a confident, correct model. Matches the forecast: near .


Example 6 — Cells G & H: the two temperature limits

Forecast: which limit turns softmax into a hard "winner takes all," and which into "everyone equal"?

Part (G) (sharpening):

  1. Scale: . The gaps are now huge.
  2. (the others are astronomically smaller). Why? As the largest score dominates completely — softmax becomes argmax (a hard pick). Cell G.

Part (H) (flattening):

  1. Scale: . Gaps are crushed to near-zero.
  2. , , . Sum .
  3. Why? As every score looks equal — softmax uniform . Cell H.

Verify: (G) gives , (H) gives ✓ — exactly the two extremes. This is why is chosen and even learned: it sits between "over-confident" and "clueless."

Figure — Multimodal models (vision-language)

Example 7 — Cell I: real-world word problem (zero-shot retrieval)

Forecast: guess the winner from the arrows (all in the same up-right region wins).

  1. Lengths: ; ; ;
  2. Similarities:
    • sunset:
    • car:
    • cat: Why this step? Zero-shot retrieval = rank candidates by cosine; no classifier trained.
  3. Softmax, : scale to ; exponentiate and normalize

Verify: highest similarity is "sunset over mountains" () — matches the forecast. The negative cat score confirms opposite-direction arrows are actively rejected. Product decision: return the sunset caption, confidence . Units check: similarities dimensionless in ✓, probabilities sum to ✓.


Example 8 — Cell J: exam twist (noisy pairs + symmetric loss)

Forecast: with only a gap, guess whether is barely above or strongly above.

  1. Scale: ,
  2. Exponentiate: ,
  3. Normalize: Why? Even a small edge, magnified by and , becomes a vs preference — the "signal accumulates" claim from the parent, made concrete.
  4. Symmetry check: if the similarity matrix is symmetric (), then image→text row and text→image column are the same numbers, so , and the averaged loss equals either one. Why the trap: students assume symmetry is automatic — it is not; the two losses coincide only when the score matrix is symmetric, which contrastive setups do not guarantee, so the average genuinely matters.

Verify: ✓ — noisy but still correctly biased. Loss . This is the mathematical reason billions of noisy pairs beat millions of clean ones.


Recall Self-test (reveal after guessing)

What operation makes cosine similarity ignore an arrow's length? ::: Dividing the dot product by both magnitudes . A zero vector's cosine similarity is... ::: Undefined () — it has no direction. As , softmax becomes... ::: A hard argmax (winner takes all). As , softmax becomes... ::: Uniform, probability each. Why does the loss average image→text and text→image? ::: Because the similarity matrix need not be symmetric, and both directions must be correct.

See also: 6.1.04-Word-embeddings, 6.5.06-Text-to-image-generation.