6.5.5 · D4Research Frontiers & Practice

Exercises — Multimodal models (vision-language)

1,929 words9 min readBack to topic

This page is a self-testing ladder for Multimodal models (vision-language). Each problem has a hidden full solution. Try first, then reveal. Levels climb from recognising the pieces to synthesising a whole training loop.

Before we start, one picture to fix all the vocabulary in your head — everything below refers back to it.

Figure — Multimodal models (vision-language)

Level 1 — Recognition

L1.1

Match each term to its one-line job: (a) vision encoder, (b) text encoder, (c) joint embedding space, (d) cosine similarity.

Recall Solution
  • (a) Vision encoder — turns a grid of pixels into a feature vector. (See 6.5.03-VisionTransformers.)
  • (b) Text encoder — turns a sequence of word-tokens into a feature vector. (See 6.1.04-Word-embeddings.)
  • (c) Joint embedding space — the single -dimensional space both vectors are pushed into, so they can be compared.
  • (d) Cosine similarity — the number in telling us how aligned two vectors' directions are.

L1.2

Cosine similarity of two vectors is . What does that mean geometrically? What about and ?

Recall Solution
  • ::: the arrows point in exactly the same direction (angle between them ).
  • ::: the arrows are perpendicular (angle ) — unrelated concepts.
  • ::: the arrows point exactly opposite (angle ). Cosine ignores length: only the angle matters.

L1.3

In CLIP training, a batch of pairs produces a similarity matrix. What is its shape, and what lives on the diagonal?

Recall Solution
  • Shape ::: (every image scored against every text).
  • The diagonal entry is the score of image against its own correct caption — these are the ones we want largest.

Level 2 — Application

L2.1

Compute the cosine similarity of and .

Recall Solution

WHAT: plug into the formula Dot product (multiply matching entries, add): . Norms (length ): for both. Very aligned — makes sense, the arrows are almost on top of each other.

L2.2

Same , but now . Compute the cosine. Interpret the sign.

Recall Solution

Dot: . Zero means the arrows are perpendicular — the model treats these as unrelated concepts. Notice the sign of the dot product carried the whole story; the norms are always positive.

L2.3

A dog image scores against three text prompts with raw similarities for . Which class is predicted in zero-shot classification, and why does no training of a classifier occur?

Recall Solution

Predicted class: dog (highest similarity ). Why no classifier: the class names are just fed through the text encoder as ordinary text. We compare the image vector to each class-name vector and pick the closest. The alignment learned during contrastive pre-training does all the work — this is transfer via a shared space, not a trained output head.


Level 3 — Analysis

L3.1

For a batch of , the dog-image row of the similarity matrix is (columns dog/cat/car/tree) with temperature . Compute the probability the model assigns to the correct pair, then the loss .

Recall Solution

WHY temperature: dividing each score by (a small number) blows the gaps up, making softmax sharp. Logits: , , , . Exponentiate: , , , . Tiny loss — the model is already confident and correct.

L3.2

Redo L3.1 with (no sharpening). Compare the correct-pair probability with the case and explain what temperature controls.

Recall Solution

Logits are just the raw similarities: . Exponentiate: , , , . Sum . With the correct probability drops to — the distribution is soft. Small → sharp/confident; large → soft/uncertain. That is why is a learned knob controlling how hard the model must commit. See 5.3.03-Cross-entropyloss for the softmax underneath.

L3.3

The full CLIP loss is symmetric: . Explain why dropping one direction permits a degenerate solution.

Recall Solution

The image→text term only forces each image to pick its right caption among captions. A model could satisfy it by mapping every text vector to the same point and just spreading image vectors — image rows still classify correctly, but text is collapsed and useless. The text→image term punishes exactly this (each text must pick its right image among images). Requiring both directions removes the shortcut. This is a stability argument straight from 6.3.02-Self-supervised-learning.


Level 4 — Synthesis

L4.1

An image of side is fed to a ViT with patch size . How many patches ? Add one extra [CLS] token — what sequence length enters the transformer? (Background: 6.5.03-VisionTransformers.)

Recall Solution

With the extra [CLS] token the sequence length is .

L4.2

You have images and captions, embedding dimension . How many entries are in the similarity matrix, and how many individual dot products must be computed to fill it (each dot product touching numbers)?

Recall Solution

Matrix entries: . Each entry is one cosine → one dot product. So dot products. Numbers touched per dot product ; total multiply-adds across the matrix multiplications (the counts reads, counts multiplies). Key insight: cost grows as — the quadratic in batch size is why big contrastive batches are expensive.

L4.3

Design choice: Flamingo freezes its vision encoder and language model and only trains small cross-attention adapters. If the frozen parts hold M parameters and the adapters hold M, what fraction of parameters is actually trained? Why is this attractive?

Recall Solution

Only of weights update. Attractive because you reuse powerful pre-trained unimodal models (parameter-efficient 6.4.01-Transfer-learning), needing far less compute and data than training everything from scratch, while cross-attention injects the image information into the language stream.


Level 5 — Mastery

L5.1

Walk the entire contrastive step for a batch of : images with captions . Cosine matrix (rows = images, cols = texts) is Compute both per-image losses, both per-text losses, and the final symmetric loss .

Recall Solution

Scale by : matrix of logits .

Image→text (softmax over each ROW):

  • Row A: ; ; .
  • Row B: correct entry is (logit ): ; ; .
  • .

Text→image (softmax over each COLUMN):

  • Col a: entries (image A, correct) and ; ; .
  • Col b: entries and (image B, correct); ; .
  • .

Final symmetric loss: The picture below shows why the diagonal being largest drives the loss down.

Figure — Multimodal models (vision-language)

L5.2

Argue why ALIGN's noisy web alt-text still trains a good model, using the contrastive signal directly.

Recall Solution

Contrastive loss only needs the correct pair to score slightly higher than the wrong pairs in the same batch. A noisy caption for image is still, on average, more relevant to image than to a random image . So the gradient nudges in the right direction on average. Individual mistakes add gradient noise but do not bias the direction; over B pairs the true signal accumulates and the noise averages out. This is the self-supervised payoff: scale beats curation because the objective is relative, not absolute.

L5.3

Suppose you double the batch size . Explain, using L3, why this generally makes the loss harder to drive down and improves the learned embeddings.

Recall Solution

Each image's softmax now runs over more competing texts (the denominator gains more terms). More competitors = more chances for a distractor to score high = higher, more informative loss = stronger gradients. The model must separate its correct pair from a larger crowd, forcing finer distinctions in the embedding space. That is precisely why CLIP used enormous batches — larger gives harder negatives for free.

Recall Quick self-check

Cosine of and ::: Patches for ViT (with CLS) ::: patches, sequence length Fraction of Flamingo params trained ::: small vs large ::: small = sharp/confident, large = soft/uncertain