01An image as a sentence of patches
A Vision Transformer (ViT) reuses the transformer architecture from language models by turning an image into a sequence of tokens:
- Cut the image into P × P patches: a 224 × 224 image with 16 × 16 patches gives a 14 × 14 grid = 196 patches.
- Flatten each patch (16 × 16 × 3 = 768 values) and multiply by a learned matrix — the patch embedding — to get a D-dimensional token.
- Prepend a learnable [CLS] token whose final state is used for classification → sequence length 197.
- Add position embeddings so the model knows where each patch came from (otherwise the order of patches would be meaningless to it).
The sequence then passes through standard transformer blocks: multi-head self-attention, an MLP, layer norm and residual connections.