Patch Embedding is the process of dividing an input image into a grid of fixed-size, non-overlapping patches and linearly projecting each patch into a flat vector. This operation transforms the raw image from a 3D tensor (H, W, C) into a sequence of 2D tokens (N, D), where N is the number of patches and D is the embedding dimension, effectively converting a spatial computer vision problem into a sequence-to-sequence problem.
Glossary
Patch Embedding

What is Patch Embedding?
Patch embedding is the foundational tokenization process for Vision Transformers (ViTs) that converts a 2D image into a sequence of 1D vector tokens suitable for a standard Transformer encoder.
A learnable [CLS] token is prepended to the sequence of patch embeddings, and 1D learnable positional encodings are added to retain spatial information lost during flattening. This sequence is then fed directly into a standard Transformer encoder, allowing the model to apply global self-attention across all image regions from the very first layer, unlike CNNs which build receptive fields hierarchically.
Key Characteristics of Patch Embedding
Patch embedding is the foundational tokenization step for Vision Transformers (ViTs), converting raw 2D images into a sequence of 1D vector tokens that a transformer encoder can process.
Linear Projection of Spatial Pixels
An input image x ∈ R^(H×W×C) is reshaped into a sequence of flattened 2D patches x_p ∈ R^(N×(P²·C)), where (P, P) is the patch resolution and N = HW/P² is the resulting number of tokens. Each patch is then mapped to a latent D-dimensional embedding via a trainable linear projection E ∈ R^((P²·C)×D). This is mathematically equivalent to a 2D convolution with a stride and kernel size equal to the patch size, making it a fully learned, non-overlapping feature extractor.
Positional Encoding Injection
Unlike CNNs which inherently capture spatial structure via sliding filters, the subsequent transformer self-attention is permutation-invariant. To retain spatial inductive bias, learnable 1D position embeddings are added to the patch embeddings. This allows the model to distinguish patch_2,3 (row 2, column 3) from patch_3,2. Standard ViTs use absolute position embeddings, while later variants explore relative positional biases or 2D-aware encodings to improve resolution generalization.
Class Token Prepending
Following BERT's design, a learnable [class] token embedding is prepended to the sequence of patch embeddings before entering the transformer encoder. The final hidden state corresponding to this token serves as the aggregate image representation y for classification heads. This avoids imposing a specific pooling structure on the patch-level outputs and allows the transformer to dynamically weigh information from all patches through self-attention.
Resolution-Agnostic Sequence Length
The number of patches N is a quadratic function of input resolution: N = (H/P) × (W/P). This creates a direct compute-performance trade-off. A 224×224 image with P=16 yields 196 tokens, while a 384×384 image yields 576 tokens. Since self-attention complexity is O(N²), higher resolutions dramatically increase memory usage. Techniques like 2D interpolation of pre-trained position embeddings are required to fine-tune models on resolutions unseen during pre-training.
Hybrid Stem Alternatives
Instead of raw pixel patches, a standard ResNet feature map can serve as the input sequence. In this hybrid architecture, the patches are formed from a lower-resolution convolutional feature map (e.g., stride-32 output), where each spatial position is treated as a 'patch' embedding. This injects stronger local inductive biases like translation equivariance directly into the token stream, often improving sample efficiency and accuracy on smaller datasets like ImageNet-1k.
Information Compression Bottleneck
The patch size P defines the granularity of the initial visual vocabulary. A small patch size (e.g., P=8) preserves fine-grained textures and small objects but generates a long sequence (e.g., 784 tokens), increasing compute. A large patch size (e.g., P=32) creates a coarse, highly compressed token sequence (e.g., 49 tokens), which is computationally cheap but risks losing small-scale structural details crucial for dense prediction tasks like semantic segmentation.
Frequently Asked Questions
Clear, technical answers to the most common questions about how Vision Transformers convert raw images into token sequences using patch embedding.
Patch embedding is the process of dividing an input image into a grid of fixed-size, non-overlapping patches and linearly projecting each patch into a flat vector, creating a sequence of tokens that a Vision Transformer (ViT) can process. The mechanism operates by reshaping an image of dimensions H x W x C into a sequence of N flattened 2D patches, where N = HW / P² and P is the patch size. Each patch is then multiplied by a learnable weight matrix E to project it into a D-dimensional embedding space. A learnable [class] token is prepended to this sequence, and 1D positional embeddings are added to retain spatial information. This transforms the raw image into a format identical to a sentence of word embeddings, allowing the standard Transformer encoder to apply multi-head self-attention across all patches.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Core concepts and mechanisms that interact with patch embedding to enable modern vision transformers and multimodal understanding.
Vision Transformer (ViT)
The foundational architecture that applies a pure transformer encoder directly to sequences of image patches. Unlike CNNs, ViT treats an image as a sequence of visual tokens, where each token is the linear projection of a flattened patch. This design removes the inductive bias of convolution, allowing the model to learn global spatial relationships through self-attention from the very first layer. ViT achieves state-of-the-art results on image classification when pre-trained on large datasets like JFT-300M or ImageNet-21k.
Positional Embedding
A critical companion to patch embedding that injects spatial location information into the sequence of patch tokens. Since self-attention is permutation-invariant, the transformer has no inherent notion of which patch belongs where. Positional embeddings—typically learned 1D vectors added to each patch token—encode the row-column coordinates of each patch in the original image grid. Without them, the model would treat the image as a bag of unordered patches, losing all spatial structure.
Linear Projection Layer
The mathematical engine behind patch embedding. For an image patch of size P × P × C (height, width, channels), this layer flattens the patch into a 1D vector of length P²C and multiplies it by a learned weight matrix W ∈ ℝ^(P²C × D) to produce a D-dimensional embedding. This is equivalent to a 2D convolution with kernel size P and stride P, followed by a flattening operation. The projection maps raw pixel intensities into a dense, semantically meaningful latent space.
Class Token ([CLS])
A learnable embedding prepended to the sequence of patch tokens, following BERT's design. This special token aggregates global image information through self-attention across all layers. After the final transformer block, the [CLS] token's representation serves as the image-level feature vector for classification tasks. It has no associated patch location and is initialized randomly, learning to pool relevant information from all patches during training.
Multimodal Transformer
An architecture that extends patch embedding to fuse visual and textual modalities. Image patches are embedded as visual tokens, while text is tokenized into word embeddings. These two token sequences are concatenated and processed by a unified transformer using cross-attention mechanisms. This enables tasks like visual question answering and image captioning, where the model must reason jointly over pixel-level and semantic-level representations.
Hierarchical Patch Merging
An evolution of fixed patch embedding used in architectures like Swin Transformer. Instead of maintaining a constant patch resolution, hierarchical models progressively merge adjacent patch tokens in deeper layers—reducing sequence length while increasing channel dimension. This creates a multi-scale feature pyramid analogous to CNN backbones, enabling efficient processing of high-resolution images and strong performance on dense prediction tasks like object detection and semantic segmentation.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us