↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading recorded inference…

What a Vision Transformer is

A transformer reads a sequence of vectors. The Vision Transformer (ViT) turns an image into one: it cuts the image into square patches, projects each to the model's width, adds a learned position vector, and prepends one extra learned token, [CLS]. From there it is the encoder of a text transformer, unchanged. There are no convolutions beyond the patch projection and no built-in sense of locality; any patch can read any other from block 1.

The page records DeiT-tiny (5,717,416 parameters) on a 224 × 224 synthetic image of a red circle, a blue square and a green triangle. With 16 × 16 patches the image becomes a 14 × 14 grid of 196 patches; with [CLS] that is 197 tokens. Twelve blocks of width 192 with 3 attention heads process them, and a classifier reads [CLS] to score ImageNet's 1,000 classes.

Patches, [CLS] and positions

Each patch is flattened to 768 numbers and multiplied by one shared matrix, 768 → 192, which is the same as a 16 × 16 convolution with stride 16. The learned [CLS] vector is prepended, and a learned position embedding, one row for each of the 197 positions, is added once:

The Patch embedding card shows the steps (“Patches · 14 × 14 · 16 × 16”, “Patch Embedding · 768 → 192”, “Position Embedding · learned · 197”). After this the model has no other notion of where a patch is: attention uses no RoPE and no relative bias, so the position rows are all it has to tell neighbouring patches from distant ones.

Patch size sets the cost. Attention compares every token with every other, so halving the patch side quadruples the number of tokens and multiplies the attention work by sixteen. With 16-pixel patches a 224-pixel image stays at 196 tokens.

Bidirectional self-attention over patches

Each block is pre-norm, with a LayerNorm before attention and before the MLP and a residual addition after each:

In the Bidirectional Self Attention card there is no causal mask: every position, [CLS] included, attends to all 197. Each of the 3 heads works on its own 64-dimensional slice of , and :

The page shows [CLS] and 5 of the 196 patches at a time, so the visible part of a row sums to less than 1. The MLP is 192 → 768 → 192 with exact GELU, , applied to every position with the same weights.

Reading out the class

After block 12 a final LayerNorm runs on every position, but the classifier reads only position 0. [CLS] carries no pixels of its own; over the 12 blocks it gathers what it needs from the patches by attention, and the Wcls box maps it to 1,000 logits:

The Class probabilities card shows the result for an image that belongs to no ImageNet class. The model splits its belief: “ping-pong ball” 10.64 % and “envelope” 10.34 %, a near tie at low confidence, then “binder” and “balloon”. These are the model's real outputs, not shape names. A classifier with a fixed label set must spread its probability over the classes it knows; the CLIP tab scores an image against captions instead.

DeiT: training a ViT on ImageNet alone

A ViT has less built in than a convolutional network. It is not told that nearby pixels belong together or that a pattern means the same thing anywhere in the image; it must learn both. Dosovitskiy et al. found that ViTs trained on ImageNet alone fell short of comparable ResNets, and that pre-training on far larger datasets reversed the order.

DeiT (Touvron et al.) closed most of that gap without extra data. Its recipe of heavy augmentation and regularisation trained a convolution-free transformer on ImageNet only, on a single computer in less than three days; its reference 86M-parameter model reached 83.1 % top-1. DeiT also introduced a distillation token: a second learned token beside [CLS] whose output is trained to match a teacher network, usually a convnet. This checkpoint is the plain DeiT-tiny without it, so its sequence is 1 + 196 = 197 tokens.

Where it came from

Before ViT, attention in vision was mostly added to convolutional networks. Dosovitskiy et al. (2020) applied a standard transformer encoder, with BERT's [CLS] read-out, directly to sequences of image patches. DeiT (2020) made it trainable at ImageNet scale, with distillation from a convnet teacher in the style of Hinton et al.

ViT encoders now sit inside many of the other tabs. CLIP's image encoder is a ViT, SmolVLM's vision encoder in the VLM tab is one, SAM and SAM 3 use large windowed ViTs, and DiT runs one over patches of an image latent. The ResNet tab in Before Transformers shows the convolutional network ViT was measured against.

Papers

  1. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale Alexey Dosovitskiy et al. · ICLR 2021 The Vision Transformer: image patches as tokens, a [CLS] read-out and learned positions, competitive with CNNs when pre-trained at scale.
  2. Training data-efficient image transformers & distillation through attention Hugo Touvron et al. · ICML 2021 DeiT, the family this page records: ViTs trained on ImageNet alone, and the distillation token.
  3. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 The transformer whose encoder ViT applies to patches unchanged.
  4. ImageNet Large Scale Visual Recognition Challenge Olga Russakovsky et al. · International Journal of Computer Vision 2015 The 1,000-class ImageNet benchmark the classifier on this page scores.
  5. Deep Residual Learning for Image Recognition Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun · CVPR 2016 ResNet, the convolutional baseline ViT and DeiT are compared with (see the ResNet tab).
  6. Distilling the Knowledge in a Neural Network Geoffrey Hinton, Oriol Vinyals, Jeff Dean · arXiv 2015 Knowledge distillation, the teacher–student training behind DeiT's distillation token.

See also