Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

~8 min readGoogle · 2020

Vision Transformer,
explained.

For a decade, computer vision meant convolutions — the CNN was the only serious way to see. Transformers had conquered language, but everyone "knew" images were different: they needed the special machinery of convolution. This paper called the bluff. Chop an image into little squares, pretend each square is a word, and feed the lot to a plain Transformer. With enough data, it worked — and the CNN's monopoly ended.

The delta

What changes against AlexNet: the inductive bias is traded for data

Every strong vision model between rung 1 and this one was convolutional, and the reason was principled: a convolution assumes that nearby pixels are related and that a cat is a cat wherever it appears. Those assumptions are true, they are baked into the architecture rather than learned, and they are why a CNN can be trained on a hundred thousand images at all.

ViT's change is to remove them. Cut the image into fixed-size patches, flatten each one, project it to a vector, and hand the sequence to an unmodified transformer encoder. At 16×16 patches on a 224-pixel image that is 196 tokens, plus one learned [class] token whose final representation is classified. Position is supplied by a learned embedding, which means the model is told that patches have an order rather than being built to know it. Nothing in the architecture knows that patch 15 is next to patch 16.

Stated in rung 1's units, the result is a genuine trade rather than a win. Trained on ImageNet-scale data alone, ViT is worse than a comparable ResNet — the paper reports this directly. Pre-trained on one to two orders of magnitude more images and then transferred, it is better, and it keeps improving as data grows where the CNN's curve flattens. So the delta is not "attention beats convolution". It is: the convolutional prior is worth a large amount of data, and once you have more data than that, it is a ceiling.

01

The CNN monopoly

Since AlexNet in 2012, convolutional neural networks owned computer vision. Convolutions bake in assumptions that fit images beautifully: locality (nearby pixels relate), translation equivariance (a cat is a cat wherever it appears), and a hierarchy from edges to shapes to objects. These "inductive biases" are why CNNs learn efficiently from modest data.

Meanwhile Transformers had taken over NLP by throwing out recurrence in favor of pure attention. The obvious question hung in the air — could attention replace convolution too? — but the consensus was no: images have millions of pixels, and letting every pixel attend to every other is computationally impossible, and Transformers lack the vision-friendly biases CNNs have baked in.

02

The big idea: an image is worth 16×16 words

The workaround is almost cheeky. You can't treat each pixel as a token — too many. So don't. Cut the image into a grid of fixed-size patches (say 16×16 pixels each). A 224×224 image becomes just 196 patches. Now treat each patch as a "word" and the image as a short sentence of a couple hundred tokens — a length Transformers handle with ease.

That's the entire trick. The paper's title says it outright: An Image is Worth 16×16 Words. Once an image is a sequence of patch-tokens, you can feed it to the same Transformer encoder that reads text, with essentially no vision-specific changes.

03

Turning patches into tokens

Each patch is a little block of pixels. Flatten those pixels into a vector and pass it through a single linear projection — a learned matrix that maps the raw pixels into a patch embedding, the visual equivalent of a word embedding. Because a Transformer has no built-in sense of order, you also add a positional embedding to each patch so the model knows where in the grid it came from.

Image → patches → tokens → Transformer
[class]patch 1patch 2…patch N→Transformer

The result is a sequence of embeddings — exactly the input a Transformer expects. From here, standard multi-head self-attention lets every patch look at every other patch, so the model can relate the top-left of an image to the bottom-right in a single layer, something a CNN only achieves after stacking many layers.

The counting is exact. Split an H×W image into P×P patches and you get a fixed grid, each patch a flattened pixel vector — for the canonical ViT-Base/16 on a 224×224 image:

But that grid size isn't free, because self-attention is O(N²) in the token count. Want finer detail from smaller patches? Every halving of the patch side quadruples the tokens and so 16×es the attention compute:

Patch size is the dial that trades spatial resolution against a quadratic compute bill — the single most important knob in a ViT.

04

The [class] token

How do you get a single label out of a bag of patch representations? ViT borrows BERT's trick: prepend one extra learnable token — the [class] token — to the sequence. It carries no pixels of its own; it's a blank slot whose job is to gather information.

As the Transformer's layers mix information through attention, this token accumulates a summary of the whole image. After the final layer, you read off its vector and attach a small classification head to predict the label. One token in, one summary out — clean and architecture-agnostic.

05

The catch: it's hungry for data

There's no free lunch. By discarding convolution, ViT also discards its helpful biases. It doesn't assume nearby patches are related or that objects can move — it has to learn all of that from examples. On a mid-sized dataset like ImageNet alone, ViT actually loses to a comparable CNN, precisely because the CNN's baked-in assumptions are a head start.

The magic appears at scale. Pre-train ViT on a truly enormous dataset — hundreds of millions of images (JFT-300M) — and it not only catches up but surpasses the best CNNs. With enough data, the model learns the right visual structure itself, and its freedom from rigid biases becomes an advantage rather than a handicap.

The trade-off

Inductive bias vs. data. CNNs encode strong priors, so they need less data but are constrained by those priors. ViT encodes almost none, so it needs a lot of data — but given that data, it can learn better representations than the priors would allow.

07

The limits

ViT's dependence on gigantic pre-training datasets was a real barrier — few teams have JFT-300M. The plain design also struggles with high-resolution images and dense tasks like detection and segmentation, where the fixed patch grid is coarse. And self-attention over many patches is quadratic in cost, which bites as images (and patch counts) grow.

Follow-up work rushed to fix these: DeiT showed ViT could be trained data-efficiently with distillation; hierarchical designs like the Swin Transformer reintroduced locality and multi-scale structure for detection and segmentation. Pure ViT was a proof of concept more than a final answer.

08

Why it still matters

ViT broke the assumption that vision needs its own special architecture, and in doing so it helped unify AI around one backbone: the Transformer now processes text, images, audio, and video with the same core machinery. That convergence is why multimodal models feel natural — everything is just a sequence of tokens.

Its fingerprints are everywhere downstream: CLIP's image encoder, the vision half of multimodal LLMs, and modern image and video generators all lean on Transformer-over-patches. When a single model can look at a picture and read a caption in the same representation space, it's because ViT showed a patch could be a token.

The bigger picture

Text became tokens (Attention), images became patch-tokens (ViT) — once everything is a sequence of tokens, one architecture can fuse them. ViT is a key step toward the multimodal models we use now.

How it works

How ViT turns an image into a sequence and classifies it, step by step

ViT follows the original Transformer encoder as closely as it can; almost everything specific to images happens before the first attention layer and after the last one. The steps below use the base model at 224×224 resolution with 16×16 patches.

224×224 image 196 patches of 16×16 linear E 768 to D values + position embedding on each cls 1 2 3 … 196 197 tokens, each a D-vector LN MSA + LN MLP + head reads [cls] ViT-B/16: 12 blocks, D = 768, 12 heads
The image becomes 196 patch tokens plus a class token, position embeddings are added, and each encoder block normalizes first and adds its output back through a residual path.
  1. Cut the image into patches. An H×W image with C channels becomes N = HW/P² flattened patches, each a vector of P²·C raw pixel values. At 224×224 with P = 16, that is 196 patches of 16·16·3 = 768 numbers each.
  2. Project each patch. One learned matrix E maps every flattened patch to a vector of width D. The same E is used for every patch, so this is a convolution with kernel and stride equal to the patch size. D is fixed for the whole network: 768 for the base model.
  3. Prepend a [class] embedding. A learnable vector is placed at the front of the sequence, making N + 1 = 197 tokens. It carries no pixels; the final state at its position is the image representation.
  4. Add position embeddings. A learned table of N + 1 vectors is added to the sequence, one per position in raster order. The model is not told that patch 15 sits next to patch 16 or above patch 31; it learns that from data. The authors tried 2D-aware variants and saw no significant gain over the plain 1D table.
  5. Run L encoder blocks. Each block applies layer normalization, multi-head self-attention and a residual connection, then layer normalization, a two-layer MLP with a GELU in between and a second residual connection. Normalization comes before each sub-layer, which is the pre-norm arrangement that trains more stably than the original post-norm layout. The MLP hidden width is 4D.
  6. Read out the class token. The final state of the [class] position is normalized and passed to a classification head. At pre-training time the head is an MLP with one hidden layer; at fine-tuning time it is replaced by a single zero-initialized linear layer with one output per downstream class.

The three standard sizes

Base has 12 layers, width 768, MLP width 3,072, 12 heads and 86 million parameters. Large has 24 layers, width 1,024, MLP width 4,096, 16 heads and 307 million. Huge has 32 layers, width 1,280, MLP width 5,120, 16 heads and 632 million. A name such as ViT-L/16 means the Large model with 16×16 patches; ViT-H/14 uses 14×14 patches, so it sees longer sequences. A hybrid variant feeds feature maps from a ResNet into the same encoder instead of raw patches.

Training and fine-tuning

Pre-training uses Adam with a batch size of 4,096, a weight decay of 0.1, which the authors found helped transfer for every model, and a linear warmup and decay, at 224×224 resolution on ImageNet, ImageNet-21k (about 14 million images) or JFT-300M (about 300 million). Fine-tuning uses SGD with momentum and a batch size of 512, usually at a higher resolution: 384 by default, and 512 or 518 for the headline ImageNet results. The patch size stays fixed, so a higher resolution means a longer sequence. At 384 with P = 16 there are 576 patches plus the class token, 577 tokens in all.

The pre-trained position embeddings no longer match a longer sequence, so they are resized by 2D interpolation according to each patch's location in the original image. This step and the patch extraction are the only places the model is handed anything about the 2D structure of images.

Inspecting the trained model shows what the missing prior turned into. Some attention heads already span most of the image in the lowest layers, a global view a CNN builds only after many layers, while other heads stay local; the average attention distance grows with depth.

The math

The ViT encoder equations

z0 = [ xclass ; xp^1E ; xp^2E ; … ; xp^NE ] + Epos, E ∈ ℝ^(P²·C)×D, Epos ∈ ℝ^(N+1)×D
z′ℓ = MSA( LN( zℓ−1 ) ) + zℓ−1, ℓ = 1 … L
zℓ = MLP( LN( z′ℓ ) ) + z′ℓ, ℓ = 1 … L
y = LN( zL^0 )
  • xp^i — the i-th flattened patch, a vector of P²·C pixel values; E is the shared linear projection to width D.
  • xclass, Epos — the learnable class embedding and the position table, one row per token including the class token.
  • MSA, MLP, LN — multi-head self-attention, a two-layer MLP with GELU, and layer normalization. Normalization is applied before each sub-layer and the residual is added after it.
  • zL^0 — the final state at position 0, the class token, which becomes the image representation y fed to the classification head.

For ViT-B/16 at 224×224, E has 768 × 768 = 589,824 entries and Epos has 197 × 768 = 151,296. Attention compares every token with every other, so its score matrix has (N + 1)² entries: 38,809 at 224 resolution and 332,929 at 384, about 8.6 times more.

The evidence

What ViT measured: transfer results, data scale and compute

The paper pre-trains on ImageNet-1k, ImageNet-21k or JFT-300M, fine-tunes on ImageNet, CIFAR, Oxford Pets and Flowers, and the 19-task VTAB suite, and compares against BiT-L (a large ResNet pre-trained on JFT) and Noisy Student (an EfficientNet trained with extra unlabeled data). Cost is counted in TPUv3 core-days of pre-training.

BenchmarkViT-H/14 (JFT)ViT-L/16 (JFT)ViT-L/16 (21k)BiT-LNoisy Student
ImageNet88.5587.7685.3087.5488.4
ImageNet ReaL90.7290.5488.6290.5490.55
CIFAR-10094.5593.9093.2593.51—
VTAB, 19 tasks77.6376.2872.7276.29—
Pre-training cost (core-days)2.5k0.68k0.23k9.9k12.3k

ViT-H/14 matched or beat both baselines while using roughly a quarter of BiT-L's pre-training compute and a fifth of Noisy Student's. The authors caution that optimizer, schedule and weight decay also change efficiency, so part of the gap is not architecture alone.

Ablations

  • Dataset size. Pre-trained on ImageNet-1k alone, ViT-Large did worse than ViT-Base and BiT beat both; on ImageNet-21k the two sizes were similar; only with JFT-300M did the larger model pay off. On random JFT subsets of 9, 30, 90 and 300 million images, ViT-B/32 did much worse than a ResNet50 at 9 million and better at 90 million and above.
  • Compute. In a controlled study on JFT, ViT reached the same accuracy as ResNets with roughly 2 to 4 times less compute. Hybrids helped slightly at small budgets and the difference vanished at larger ones. ViT did not saturate in the range tried.
  • Position information. Giving the model some position embedding mattered; 1D, 2D and relative variants differed little.
  • Class token. Global average pooling over patch outputs worked as well as the class token once the learning rate was retuned.
  • Self-supervision. A preliminary masked-patch objective brought ViT-B/16 to 79.9 percent on ImageNet, 2 points above training from scratch and 4 below supervised pre-training.
Since then

What the authors admit, and what happened next

The data appetite is stated as a finding rather than buried — the paper's own framing is that large-scale pre-training is what makes the approach work, and that below that threshold the missing inductive bias costs you. It also notes the practical consequence: attention over patches is quadratic in the number of patches, so higher resolution is expensive in a way convolution is not.

The data-hunger claim was then substantially weakened by better training, which is the most useful thing that happened to this paper. Touvron and colleagues' DeiT recipe made a ViT competitive on ImageNet-1k alone using heavy augmentation — mixup and CutMix among them — plus distillation from a convolutional teacher. Read together, the two results say the original ceiling was partly an artefact of the training recipe rather than a property of the architecture, which is a caution worth carrying to every other "needs enormous data" claim on this site.

How it held up: the patch-plus-transformer backbone is now the default for anything that has to put images and text in the same representation — it is the image tower in CLIP and the visual encoder feeding LLaVA. Pure global attention did not win unchallenged: hierarchical designs such as Swin reintroduced locality and multi-scale windows and remain strong for dense prediction, which suggests the convolutional prior was right about something even where it was not required. And the [class] token itself is largely gone — pooling over patch tokens works as well or better, and rung 5's features combine the class token with pooled patch tokens.

SOLVE IT YOURSELF

Tokenize an image and price the attention

"An image is worth 16×16 words" becomes literal when you count the words. This turns image size and patch size into a token count — landing on ViT-Base/16's exact 196 patches, 197 tokens, 768-dim patch vectors — and then prices self-attention, which is quadratic in that count. Shrink the patch for finer detail and watch the attention bill explode 16×. Change the resolution or patch size and see which configs you could actually afford to train.

YOUR TASK

Write the body of attn_cost. Everything else in the file is already written; this is the function the paper is about. Press ▶ Run to check it against the 5 assertions below — they start red.

WHAT IT HAS TO SATISFY — 5 ASSERTIONS
  1. A 224x224 image with 16x16 patches yields 196 patches
  2. The sequence adds one [class] token
  3. A 16x16x3 patch flattens to 768 pixel values
  4. Halving the patch side quadruples the tokens
  5. Attention is quadratic: 4x tokens is 16x cost
CPython · WebAssembly
Questions

Questions people ask about Vision Transformer

What are the ViT-Base, ViT-Large and ViT-Huge configurations?

The three sizes follow BERT's naming. ViT-Base has 12 encoder layers, hidden width 768, MLP width 3,072, 12 attention heads and 86 million parameters. ViT-Large has 24 layers, width 1,024, MLP width 4,096, 16 heads and 307 million parameters. ViT-Huge has 32 layers, width 1,280, MLP width 5,120, 16 heads and 632 million parameters. The suffix gives the patch size, so ViT-B/16 uses 16 by 16 patches and ViT-H/14 uses 14 by 14. A smaller patch means more tokens per image, which raises compute faster than the parameter count suggests because attention cost grows with the square of the sequence length.

How does ViT adapt position embeddings when fine-tuning at a higher resolution?

The patch size stays fixed, so a larger image produces more patches and a longer sequence than the learned position table covers. The authors resize the pre-trained table with 2D interpolation, treating each embedding as sitting at its patch's location in the original grid and resampling to the new grid. For 16 by 16 patches, going from 224 to 384 pixels grows the grid from 14 by 14 to 24 by 24, so 576 positions plus the class token. The paper notes that this resizing and the patch extraction are the only places where knowledge of the images' 2D structure is injected by hand.

Why does ViT lose to ResNets on ImageNet alone but win after JFT-300M pre-training?

A convolution builds in two assumptions: nearby pixels matter together, and the same pattern means the same thing wherever it appears. ViT has neither, apart from the patch cut, so it must learn them from examples. With about 1.3 million ImageNet training images it overfits more than a ResNet of similar cost, and in the paper's JFT subset test a ViT-B/32 was far behind a ResNet50 at 9 million images but ahead from 90 million. With 300 million images, learning the patterns directly was enough, and even an advantage. Later work on stronger augmentation narrowed the gap on ImageNet alone.

Does ViT need the class token, or can it use average pooling?

It does not strictly need it. The authors included the extra token to stay as close as possible to the original Transformer and to BERT. When they compared it with global average pooling over the patch outputs, both worked about equally well, but each needed a different learning rate, and the difference in accuracy was fully explained by that learning-rate mismatch. Many later systems drop the class token and pool, or use attention pooling, especially when patch features are needed for dense tasks such as segmentation. The class token mainly gives a clean single position at which to attach a classification head.

Why does ViT put layer normalization before attention and the MLP?

In the ViT encoder, layer normalization is applied to the input of each sub-layer, and the residual connection adds the unnormalized stream back afterward. This pre-norm arrangement keeps the residual path an untouched sum of every block's output, which makes very deep stacks easier to optimize than the original Transformer's post-norm layout, where normalization sits on the main path. The paper describes the layout without ablating it, so the stability argument comes from the wider Transformer literature rather than from a ViT experiment. The final output vector is normalized once more before it reaches the classification head.

Check yourself

4 questions to test what you just read

Work each one out before you open it — a number, a prediction or a reason, not a definition. Producing the answer is what makes it stick. Your progress is kept on this device.

A ViT-L/16 is fine-tuned at 512×512. How many tokens does the encoder see, and how does its attention matrix compare with the 224×224 case?

Show answer

At 512 with P = 16 there are 32 × 32 = 1,024 patches plus the class token, 1,025 tokens. At 224 there are 197. The attention matrix grows from 197² = 38,809 to 1,025² ≈ 1.05 million entries, about 27 times larger.

ViT-H/14 cost 2.5k TPUv3 core-days and BiT-L cost 9.9k. Roughly how many times more pre-training compute did BiT-L use, and why should that ratio be read cautiously?

Show answer

About 9.9 / 2.5 ≈ 4 times more. The authors warn that training schedule, optimizer and weight decay also affect efficiency, so the ratio mixes architecture with training choices rather than isolating the architecture.

On a 9-million-image pre-training subset ViT-B/32 trails a comparable ResNet, but at 90 million it leads. What does that crossover say about convolution's role?

Show answer

The convolutional prior substitutes for data. With little data the ResNet's built-in locality and translation structure helps; with enough data ViT learns equivalent patterns itself and keeps improving, so the prior eventually becomes a limit.

ViT-B/16 at 224×224 uses a learned position table. How many numbers does it hold, and what changes when you fine-tune at 384?

Show answer

197 × 768 = 151,296 numbers. At 384 the sequence has 577 tokens, so the table is resized by 2D interpolation of the 14 × 14 patch grid to 24 × 24.

Where this sits

Learning to see

From the network that started the deep-learning era to one you can segment anything with — and the shift from labels to self-supervision in between.

  1. 1AlexNet2012
  2. 2Vision Transformer2021
  3. 3SimCLR2020
  4. 4Masked Autoencoders2022
  5. 5DINOv22023
  6. 6Segment Anything2023

All 110 papers, in 19 ladders →

An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale · Dosovitskiy, Beyer, Kolesnikov, et al. · ICLR 2021 · read the original paper on arXiv → · Vibe Engines · 2026
Finished this one? 0 / 110 Paper Breakdowns done
NextSimCLR Next on the “Learning to see” ladder · rung 3 of 6

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns