Vision Transformer › Patch Embeddings and the CLS Token
Language AI HandbookAll chapters Vision Transformer › Patch Embeddings and the CLS TokenOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Once we have flattened the image patches into vectors, we need to project them into the transformer's embedding dimension . This process is analogous to word embeddings in NLP, where we map discrete vocabulary indices to dense continuous vectors. In ViT, we apply a trainable linear projection to each patch vector.
The embedding for the -th patch is computed as:
where:
- : the embedding vector for the -th patch after projection
- : the flattened -th patch (input vector), where is the patch size and is the number of channels
- : the trainable linear projection matrix mapping from patch dimension to embedding dimension
- : the patch index, ranging from to (total number of patches)
This linear projection is a learned weight matrix, initialized randomly and trained end-to-end along with the rest of the model. Conceptually it serves the same purpose as an embedding table in NLP: it maps the raw input representation to a space where meaningful relationships can be captured and transformed by the downstream attention layers. Unlike a discrete embedding table that has one learned vector per vocabulary entry, the patch embedding matrix is a general linear transformation applied uniformly to every patch, regardless of its content. The model must therefore learn a transformation that works for all possible patches it might encounter, a more challenging requirement that again explains why more pre-training data is beneficial.
An important practical equivalence exists between this explicit flatten-then-project operation and a convolutional layer applied with stride and no padding. Both operations extract non-overlapping windows of size , apply the same linear map to each window, and produce a spatial grid of feature vectors. ViT implementations therefore sometimes use nn.Conv2d(in_channels=3, out_channels=D, kernel_size=P, stride=P) as the patch embedding layer, which is computationally equivalent to the explicit formulation but benefits from highly optimized convolution routines on modern hardware.
The Classification Token
Following the convention established by BERT (which we covered in BERT Architecture), ViT prepends a special learnable embedding to the beginning of the sequence. This classification token (CLS token) serves a unique role in the architecture. It begins as a learnable parameter initialized randomly, with no inherent content. As information flows through the transformer layers via self-attention, the CLS token can attend to every patch in the sequence, aggregating global information from all spatial locations.
Why use a separate CLS token for classification rather than, say, averaging all patch representations? The averaging approach, called global average pooling, is also used in practice and works well. The CLS token approach is conceptually cleaner for sequence classification because the model can learn to selectively aggregate the information that is most relevant for the classification decision, rather than uniformly combining all spatial information. Some patches might be background clutter that contributes little to the class label; a learnable aggregation via the CLS token can in principle learn to downweight such patches. In practice, both approaches yield similar accuracy, and the choice is sometimes made for architectural consistency with BERT-style models.
Position Embeddings and the Full Input Sequence
After prepending the CLS token, we add learned position embeddings to inject spatial information. The complete input sequence to the transformer is:
where:
- : the complete input sequence to the transformer, with position embeddings added element-wise
- : the learnable CLS token embedding prepended to the sequence at position 0
- : the linearly projected -th patch embedding
- : the matrix of position embeddings, with one row per sequence position including the CLS token
- : vertical concatenation (stacking vectors row-by-row to form a matrix)
- : total number of patches ( in the running example)
The here denotes element-wise addition: each row of the concatenated patch matrix is added to the corresponding row of the position embedding matrix. This means every token in the sequence, including the CLS token, receives a position-specific bias vector that encodes its index in the sequence. After this addition, the model's self-attention layers can use these position signals to infer spatial relationships.