A shared embedding space lets you measure distance between a whole image and a whole caption. But a caption is made of individual words, and an image is made of individual regions — what if a word could look at specific parts of the image instead of the image as one averaged point?
This is Part IV's attention mechanism again, but now comes from a text token while and come from image patches — sky, ground, and dog. Drag the query: it's still one attention computation, just spanning two modalities instead of one sequence attending to itself.
The formula hasn't changed at all from Part IV:
- — the query vector, coming from a text token.
- — the key vector for image patch .
- — the raw compatibility between the query and image patch , before normalizing.
- — the attention weight on image patch , after softmax turns the scores into a distribution.
- — the weighted blend of all image patches that the query actually attends to.
- — the dimensionality of the key vectors, used to rescale the raw scores.
- Self-attention: everything from one sequence
In Part IV, , , and all came from the same sequence attending to itself.
- Cross-attention: q and k/v from different modalities
Here comes from one modality's encoder (text) while come from a completely different one (image patches) — the same mechanism, just pointed across a modality boundary instead of within one sequence.
Watch the context vector as you drag. It's always a weighted blend of all three image patches — never any single one exactly, unless one weight reaches all the way to . This is what a text token "looking at" an image actually produces: not a chosen patch, but a soft mixture shaped by how much each patch matters to that particular word.
At query over patches sky, ground, dog:
- Raw scores, scaled by root d
Dot product with each key, then divide by :
- Sky: , so
- Ground: , so
- Dog: , so
- Softmax into weights
Exponentiate each and divide by their sum :
- Sky: , so
- Ground: , so
- Dog: , so
The ground patch dominates, since this query leans more toward "ground" than "sky" or "dog" in raw dot product.
- The context vector
Weighting each patch by its share and summing:
- Sky:
- Ground:
- Dog:
Summing componentwise: — noticeably closer to the ground patch's own coordinates than to sky's, but pulled partway toward the other two, exactly reflecting their smaller but nonzero weights.
Drag the query until the sky patch receives more than 0.7 of the attention weight.
Cross-attention is what lets a caption-generating model ask, word by word, "which part of the image is relevant to the word I'm about to produce" — instead of summarizing the whole image into one fixed vector up front and hoping every word can work from that. The next chapter assembles this alongside Chapter 1's joint embeddings into one working vision-language pipeline.