Explain the Vision Transformer (ViT). How does it convert an image into a class prediction?
Answer
A Vision Transformer converts an image into a sequence of fixed-size patch tokens and processes them with a Transformer encoder. A learned class token is prepended, positional embeddings preserve patch order, and global self-attention lets every patch exchange information with every other patch. After the encoder stack, the class-token representation is passed to a prediction head. Compared with a CNN, a ViT has weaker built-in locality and translation bias, but it can model long-range interactions directly and scales effectively with data and compute.

Figure 1: ViT architecture with tensor shapes, patch tokenization, the Transformer encoder stack, and the classification path.
(1) Tokenization: An image of shape is divided into
non-overlapping
patches; each flattened patch is projected to width
.
(2) Global Context: Multi-head self-attention mixes information across all patch positions, while residual connections and MLP sublayers refine each token.
(3) Classification: A learned [CLS] token aggregates evidence across the encoder stack; its final state is normalized and mapped to class logits.

Figure 2: ViT inference flow from image validation and patch embedding to encoder processing and class prediction.
Mathematical Formulation:
Where:
is the initial token sequence supplied to the Transformer encoder.
is the learned class token, and
is flattened image patch
.
projects each
patch to width
, while
supplies positional embeddings.
is the patch count for an image of height
, width
, and channel count
.
,
, and
are query, key, and value matrices;
is the per-head query/key width.
Leave a Reply