14 August 2026
Vision Transformer
I used CNNs for a long time before I really read An Image is Worth 16x16 Words, the paper that introduced the Vision Transformer (ViT). I used to think the story was "attention is just stronger than convolution". After reading it properly, the real story is different: ViT turns an image into tokens and then uses an almost unchanged Transformer encoder. CNNs have strong built-in assumptions about images, so they learn faster from small data. ViT has fewer assumptions, so it needs more data, but it scales better.
The core idea
An NLP Transformer takes a 1D sequence of tokens. ViT turns a 2D image into a 1D sequence of visual tokens:
- Split a
224 × 224 × 3image into a14 × 14grid of patches. - Flatten each patch into numbers.
- Project each patch to a -dimensional token with one shared linear layer.
- Add a
[CLS]token at the front and add position embeddings. - Run Transformer encoder blocks (attention + MLP).
- Read the final
[CLS]token and send it to a classification head.
The /16 in a name like ViT-B/16 means the patch size is . It is not the number of layers.
Patch embedding
Patches solve the sequence length problem. If every pixel of a 224 × 224 image were a token, there would be 50,176 tokens, and attention costs . With patches:
One learnable matrix projects every patch. With the [CLS] token and position embeddings:
So ViT-B/16 actually feeds 197 tokens into the encoder: 196 patches plus one [CLS]. Each token is a -dimensional vector.
How [CLS] works
(B, 1, D) + (B, 196, D) → (B, 197, D)
CLS patches
All 197 tokens are updated together. Through self-attention, [CLS] collects information from every patch, like a summary of the whole image being filled in layer by layer. At the end we take x[:, 0, :] and send it to the classifier. The loss only reads [CLS], but the gradient still flows back through attention to every patch embedding.
What the linear layer looks like in code
You can either cut out patches and use Linear(P²C, D), or use Conv2d(C, D, kernel_size=P, stride=P). They are the same thing: one set of weights applied to every non-overlapping patch. In ViT-B/16, also happens to equal . That is a coincidence of this config, not a rule.
The encoder
ViT reuses the standard Transformer encoder: pre-LayerNorm, multi-head self-attention, a two-layer GELU MLP, and residual connections.
The paper uses learnable 1D position embeddings, so the model is not told about up, down, left and right. It has to learn those relationships from data. When fine-tuning at a higher resolution, the patch size stays the same, the number of tokens grows, and the position embeddings are interpolated in 2D.
Why CNNs win with less data
Inductive bias is an assumption built into the architecture. When the assumption is right, the model does not have to rediscover it, so it needs fewer examples.
| CNN | ViT | |
|---|---|---|
| Locality | each layer only looks at a small neighbourhood | attention can see the whole image from layer one |
| 2D neighbours | built into the kernel | mostly learned |
| Translation | weight sharing gives it for free | no equal built-in guarantee |
| Receptive field | grows layer by layer | global in every attention layer |
| Prior | strong, more restrictive | weak, more flexible, needs more data |
So on smaller datasets ViT overfits more easily and CNNs often do better. In the paper's experiments on subsets of JFT, ResNets won when the data was small, and ViT overtook ResNets of similar compute at around 90M+ images. My takeaway: with little data, the right assumptions are an advantage. With a lot of data, letting the model learn the rules itself may have a higher ceiling.
Hybrid ViT
A hybrid model first runs a CNN to get a feature map, then treats each position of the feature map as a token (like using 1 × 1 patches). The CNN handles local edges and textures, and the Transformer handles global relationships. In the paper, hybrids were slightly better at small compute budgets, and the gap disappeared as models grew.
Data and scaling
The core trade-off is fewer built-in assumptions in exchange for better scaling:
- Pretrained on ImageNet, large ViTs did worse than smaller ViTs and worse than BiT ResNets.
- On ImageNet-21k, the gap got smaller.
- On JFT-300M, big ViTs finally pulled ahead, and needed about 2 to 4× less pretraining compute to reach the same transfer performance.
Patch size matters too. Going from 16 to 8 makes 4× more tokens, and the part of attention becomes about 16× more expensive.
Is ViT more robust than CNNs?
This part does not come from the original ViT paper. It comes from a later paper, Intriguing Properties of Vision Transformers (Naseer et al., NeurIPS 2021), and the results depend on the models and benchmarks they tested:
- Occlusion: when random patches were dropped, ViT/DeiT lost accuracy more slowly than ResNet50. With 80% of the image hidden, DeiT still kept about 60% ImageNet top-1. Later attention layers moved to the patches that were still visible.
- Texture vs shape: on Stylized ImageNet, which changes textures but keeps shapes, ViT relied more on shape than similar CNNs.
- Adversarial patches: in their tests ViT was harder to fool with one local patch. That is a relative result, not a security guarantee.
- Shuffled patches: DeiT kept more accuracy than CNNs when patches were moderately shuffled, because it can rely more on what is there than where it is.
Things I got confused about
14 × 14is the patch grid and16 × 16is the pixel size of each patch. There are 196 patches.- Patch embedding does not "understand" a patch. It starts as a linear projection, and meaning builds up through the layers.
- ViT is not completely free of inductive bias. Patches, shared projection and position embeddings are all assumptions. It just has far fewer image-specific ones than a CNN.
My mental model
A CNN says: assume local structure matters, then build up to the whole picture.
A ViT says: turn the image into tokens, let every patch see everything from the first layer, and learn from lots of data which relationships matter.