Chapter 3 fed cross-attention a handful of ready-made "image patch" vectors and never asked where they came from. A raw image is just a grid of pixel numbers — how does anyone turn that grid into vectors a text-trained attention mechanism can even read?
That cell belongs to the top-left patch — pixels [1, 2, 3, 4] flattened into one row.
This tiny picture is a 4×4 grid of pixel values. Click any cell — it belongs to one of four non-overlapping 2×2 blocks. That whole block, four raw pixel numbers, collapses into just two numbers: a patch embedding. Every one of those four blocks goes through the exact same collapsing step.
Flatten a patch's pixels into one vector , then apply a fixed weight matrix :
- — one patch's pixels, flattened row-major into a length-4 vector.
- — the projector: a fixed weight matrix, learned in a real model but hand-picked here.
- — the resulting patch embedding, a single point in the same 2D space text tokens live in.
- Patchify
Slice the image into non-overlapping blocks — here, four 2×2 blocks out of a 4×4 image.
- Flatten
Read each block's pixels out in row order, turning a 2D block into a 1D vector of length 4.
- Project linearly
Multiply by : row 0 of sums 's top-row pixels, row 1 sums its bottom-row pixels — one genuine (if simplified) linear layer, mapping 4 numbers down to 2.
- Land in a shared space
is now a point in the same 2D coordinate system as this chapter's text-token embeddings — nothing about 's shape reveals it came from pixels rather than a word.
Click through all four patches. Each one lands at a different point in the embedding space — and each point sits exactly on top of one particular text token's own embedding. That's not a coincidence built into this toy example so much as the entire goal of a real patch projector: train it so a patch of sky lands near the word "sky," a patch of dog lands near the word "dog," and so on.
The bottom-right patch's raw pixels are :
- Flatten row-major
.
- Apply the projector
- Top-row sum: .
- Bottom-row sum: .
So .
- Compare to every text token
The text token "dog" sits at exactly — distance . Every other text token ("sky," "grass," "cat") is at least units away in each coordinate. The nearest-neighbor search from Chapter 1 has an unambiguous winner.
Click the patch whose projected embedding lands nearest the text token “dog” at (27, 31).
click a cell to try it
A patch projector is nothing more exotic than a linear layer: flatten a patch's pixels, multiply by a weight matrix, and the result is a token — indistinguishable in shape from a word embedding, and directly comparable to one by ordinary distance. This is the missing piece behind every image patch this Part has been feeding into attention and joint embedding spaces: real models learn end-to-end so that patches and words that mean the same thing land in the same place. The next chapter asks what happens once a model has to generate a whole sequence of these tokens — text and image alike — one at a time.