Patch embedding is the process of dividing an input image into a grid of non-overlapping, fixed-size patches, flattening each patch into a vector, and linearly projecting it into a lower-dimensional embedding space. This operation transforms raw pixel data into a sequence of patch tokens, analogous to how text is tokenized into word embeddings in natural language processing, enabling the Transformer's self-attention mechanism to model spatial relationships.
Glossary
Patch Embedding

What is Patch Embedding?
Patch embedding is the foundational tokenization step in Vision Transformers (ViTs) that converts a 2D image into a sequence of 1D vector representations suitable for processing by a standard Transformer encoder.
A learnable class token is prepended to the patch sequence, and positional embeddings are added to retain spatial information lost during flattening. The linear projection is typically implemented as a single 2D convolutional layer with a stride equal to the patch size, efficiently performing both the splitting and embedding in one operation before the sequence enters the Transformer encoder.
Key Characteristics of Patch Embedding
Patch embedding is the mechanism that converts a 2D image into a 1D sequence of tokens, enabling transformer architectures to process visual data using the same self-attention principles originally designed for natural language.
Image-to-Sequence Conversion
The core operation divides an input image H × W × C into a grid of N non-overlapping patches of fixed size P × P. Each patch is flattened into a 1D vector of length P²·C and linearly projected to a D-dimensional embedding space. This transforms the image from a 3D tensor into a sequence of N token vectors, where N = HW/P². A learnable [CLS] token is prepended to the sequence, and 1D positional embeddings are added to retain spatial information lost during flattening.
Linear Projection Layer
Each flattened patch vector is mapped to the model's latent dimension D via a trainable linear transformation—effectively a fully connected layer without bias. This projection can be implemented as a 2D convolution with kernel size and stride equal to P, outputting D channels. The convolution approach is computationally equivalent but more efficient, performing the patch extraction and embedding in a single operation. The resulting weight matrix learns to encode low-level features like edges, textures, and color gradients directly from raw pixel values.
Positional Encoding Integration
Since self-attention is permutation-invariant, patch embeddings require explicit spatial information. Standard ViT adds learnable 1D positional embeddings to each patch token after projection. These embeddings are initialized randomly and trained end-to-end, allowing the model to discover optimal position representations. Alternative approaches include 2D sinusoidal encodings that encode row and column coordinates separately, or relative position biases added directly to attention scores. Without positional encoding, the model cannot distinguish between a patch in the top-left corner and one in the center.
Hierarchical Patch Embedding
Advanced architectures like Swin Transformer and PVT extend basic patch embedding with hierarchical multi-scale representations. Instead of a single fixed patch size, these models:
- Start with small 4×4 patches for fine-grained local features
- Use patch merging layers to concatenate neighboring patch embeddings
- Progressively reduce spatial resolution while increasing channel dimension This pyramid structure produces feature maps at multiple scales—/4, /8, /16, /32 of input resolution—making them compatible with dense prediction tasks like segmentation and detection that require high-resolution outputs.
Overlapping Patch Embedding
While standard ViT uses non-overlapping patches, some variants employ overlapping patch embedding via strided convolutions where stride < kernel_size. This creates patches that share pixels with neighbors, providing:
- Smoother spatial transitions between adjacent tokens
- Implicit local context overlap that aids boundary feature extraction
- Better preservation of fine-grained spatial relationships The trade-off is increased computational cost—more total patches and redundant computation—but the improved spatial continuity often benefits tasks requiring precise localization.
Relationship to Word Embeddings
Patch embedding is the visual analog of word embedding in NLP transformers. In text processing, words are mapped to dense vectors via a lookup table; in vision, image patches are mapped via linear projection. Both produce a sequence of D-dimensional vectors fed into identical transformer encoder blocks. This design unification is the key insight of Vision Transformers: treating images as a foreign language where each patch is a 'visual word.' The same self-attention mechanism then models relationships between patches just as it models relationships between words.
Patch Embedding vs. CNN Feature Extraction
A technical comparison of how Vision Transformer patch embedding and convolutional neural network feature extraction differ in mechanism, inductive bias, and representational properties for medical imaging analysis.
| Feature | Patch Embedding (ViT) | CNN Feature Extraction | Hybrid CNN-ViT |
|---|---|---|---|
Input Processing | Divides image into fixed-size non-overlapping patches; linearly projects each patch to token vector | Applies sliding convolutional kernels across the image to produce feature maps hierarchically | Uses CNN backbone to extract feature maps, then tokenizes feature map patches for transformer |
Inductive Bias | Low — no built-in assumptions about locality or translation equivariance; learns spatial structure from data | High — strong locality bias via small receptive fields and weight sharing enforces translation equivariance | Moderate — CNN frontend injects locality bias while transformer backend enables global attention |
Receptive Field at Layer 1 | Global — each patch token can attend to every other patch immediately via self-attention | Local — limited to kernel size (e.g., 3×3 or 5×5); global context builds gradually through depth | Local initially via CNN kernels; global after patch tokens enter transformer self-attention layers |
Parameter Efficiency | Lower for small datasets — requires large-scale pretraining due to lack of spatial priors | Higher for small datasets — parameter sharing and locality priors enable effective learning from limited data | Balanced — CNN reduces token count for transformer, improving efficiency over pure ViT |
Position Information | Requires explicit learnable or fixed sinusoidal position embeddings added to patch tokens | Implicit — spatial hierarchy preserved through feature map dimensions and receptive field progression | CNN stage preserves implicit position; transformer stage may add explicit position embeddings |
Multi-Scale Representation | Single-scale — fixed patch size produces one token per grid cell; requires hierarchical ViT variants for multi-scale | Inherent — pooling and strided convolutions naturally build pyramidal feature hierarchy | Inherent — CNN backbone provides multi-scale feature maps before tokenization |
Computational Complexity | O(N²) in self-attention where N = number of patches; 196 patches for 224×224 image with 16×16 patch size | O(K² · C_in · C_out · H · W) per layer; scales linearly with spatial dimensions | Reduced O(N²) — CNN downsampling decreases token count before transformer, lowering attention cost |
Transfer Learning Performance | Excels with large-scale pretraining (ImageNet-21k, JFT-300M); underperforms CNN on small medical datasets without pretraining | Strong transfer from ImageNet pretraining even to small domain-specific datasets due to inductive bias | Competitive — combines CNN transferability with transformer representational capacity; strong on medium-sized medical datasets |
Frequently Asked Questions
Patch embedding is the foundational tokenization step that enables Vision Transformers to process images. Below are the most common technical questions about how this mechanism works, its design trade-offs, and its role in multimodal medical AI.
Patch embedding is a tokenization technique that converts a 2D image into a sequence of 1D vector tokens suitable for a standard Transformer encoder. The process operates by dividing an input image x ∈ R^(H×W×C) into a grid of non-overlapping fixed-size patches (typically 16×16 pixels). Each patch is flattened into a 1D vector and linearly projected via a learnable matrix E to a D-dimensional embedding space. A learnable [CLS] token is prepended to the sequence, and learnable 1D positional embeddings are added to retain spatial information. The resulting sequence of patch embeddings is then fed directly into the Transformer's multi-head self-attention layers, treating each patch analogously to a word token in NLP.
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
Understanding patch embedding requires familiarity with the broader Vision Transformer architecture and the tokenization strategies that enable transformers to process visual data.
Vision Transformer (ViT)
The foundational architecture that introduced patch embedding to computer vision. A ViT splits an image into fixed-size patches, linearly embeds each patch, adds positional embeddings, and feeds the resulting sequence into a standard Transformer encoder. This design treats image patches analogously to word tokens in NLP, enabling the application of self-attention mechanisms directly to visual data without convolutional inductive biases.
Linear Projection
The mathematical operation at the core of patch embedding. Each flattened image patch is multiplied by a learnable weight matrix E to project it into a D-dimensional embedding space. For a patch of size P×P×C (height, width, channels), the projection maps from R^(P²·C) to R^D. This is equivalent to a 2D convolution with stride equal to patch size, making it efficiently implementable in standard deep learning frameworks.
Positional Embedding
A critical companion to patch embedding that injects spatial location information into the sequence. Because self-attention is permutation-invariant, the model has no inherent notion of patch order. Positional embeddings—typically learned 1D vectors added to each patch embedding—encode the original grid position of each patch, enabling the transformer to reconstruct spatial relationships and understand global image structure.
Class Token (CLS)
A special learnable embedding prepended to the sequence of patch embeddings, inspired by BERT's [CLS] token. This token aggregates global image-level information through the self-attention layers and serves as the image representation for final classification. The CLS token interacts with all patch embeddings at every transformer layer, effectively pooling information from the entire image without requiring explicit global average pooling.
Hybrid Architecture
A variant where patch embeddings are extracted from intermediate feature maps of a CNN rather than raw pixels. Instead of splitting the original image, a ResNet backbone generates a feature map, and patches are extracted from this learned representation. This combines the local feature extraction strengths of convolutions with the global reasoning capabilities of transformers, often improving data efficiency and performance on smaller datasets.
Tokenization Analogy
Patch embedding is the visual equivalent of text tokenization in NLP transformers. Just as sentences are split into word or subword tokens and mapped to embeddings, images are divided into patches and projected into vectors. This unification enables multimodal transformers to process images and text through identical architectural backbones, treating both modalities as sequences of embeddings that can interact via cross-attention mechanisms.

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