Positional encoding is a deterministic or learned vector added to the patch embeddings of a Vision Transformer (ViT) before they enter the encoder stack. Because the self-attention mechanism computes relationships between all tokens as a set—treating them without inherent order—the model would otherwise be blind to the spatial arrangement of image patches. The encoding provides a unique positional signature for each grid location.
Glossary
Positional Encoding

What is Positional Encoding?
Positional encoding is the mechanism that injects information about the spatial location of image patches into a Vision Transformer's input embeddings, enabling the permutation-invariant self-attention operation to distinguish sequence order.
Standard ViTs use learned 1D position embeddings, while advanced architectures like the Swin Transformer employ relative position biases within local windows. Alternatives such as Rotary Position Embedding (RoPE) encode position via rotation matrices applied to query and key vectors, naturally incorporating relative distance into the attention computation without additional learned parameters.
Core Properties of Positional Encodings
Positional encodings are the critical mechanism that injects spatial structure into the permutation-invariant self-attention operation, enabling Vision Transformers to understand the geometry of an image.
Permutation Invariance Problem
The standard self-attention mechanism treats input tokens as a set, not a sequence. Without positional information, shuffling image patches produces the exact same output representation. Positional encoding resolves this by adding a unique signal to each token's embedding that encodes its spatial coordinates, allowing the model to distinguish the top-left patch from the bottom-right patch and learn spatial relationships like edges and shapes.
Absolute vs. Relative Encoding
Two fundamental paradigms exist for injecting position information:
- Absolute Positional Encoding: Assigns a unique vector to each spatial coordinate (e.g., row 1, col 1). The model learns what 'position (0,0)' means globally.
- Relative Positional Encoding: Encodes the distance or vector between pairs of tokens. The model learns that 'patch A is 2 units to the left of patch B', which is often more robust to changes in image resolution or translation. Modern architectures like Swin Transformer heavily leverage relative positional biases.
Learned vs. Fixed Sinusoidal
The original Vision Transformer (ViT) uses learned 1D positional embeddings, where a parameter vector is trained for each patch index. In contrast, the original NLP Transformer used fixed sinusoidal functions of varying frequencies. While learned embeddings are simple, fixed sinusoidal encodings theoretically allow the model to extrapolate to sequence lengths not seen during training, though this property is less critical for fixed-resolution images. Rotary Position Embedding (RoPE) offers a hybrid approach, encoding absolute position via rotation while naturally capturing relative distances in the dot-product attention.
2D Spatial Structure
Images are inherently two-dimensional, but a ViT flattens patches into a 1D sequence. Naive 1D positional encodings only capture the raster-scan order. Advanced methods explicitly encode the (x, y) grid coordinates of each patch. This can be done by learning separate row and column embeddings and summing them, or by extending sinusoidal functions to two dimensions. Encoding the true 2D structure helps the model learn vertical and horizontal relationships more effectively than a simple linear ordering.
Translation Invariance vs. Equivariance
A key design tension exists between translation invariance (shifting the input doesn't change the output classification) and translation equivariance (shifting the input shifts the output segmentation map accordingly). Absolute positional encodings can make a model overly sensitive to absolute position, breaking translation invariance. Relative positional biases and the Swin Transformer's local windowed attention naturally promote translation equivariance, making them highly effective for dense prediction tasks like medical image segmentation where the exact location of a lesion is critical.
Interpolation for Resolution Changes
Medical imaging often uses varying resolutions (e.g., different CT slice thicknesses). A ViT trained on 224x224 images with learned positional embeddings cannot natively process a 512x512 image. A common practical solution is positional embedding interpolation: the pre-trained 1D or 2D grid of position vectors is resampled using bilinear or bicubic interpolation to match the new patch sequence length. This allows for effective transfer learning to higher-resolution medical images without training a new positional encoding from scratch.
Frequently Asked Questions
Clear, technical answers to the most common questions about how Vision Transformers understand spatial location and why positional encoding is critical for medical image analysis.
Positional encoding is a mechanism that injects information about the spatial location of image patches into the Transformer's input embeddings. Unlike convolutional neural networks (CNNs), which process pixels in a sliding-window manner that inherently preserves spatial structure, the self-attention mechanism in a Vision Transformer is permutation-invariant—it treats input patches as an unordered set. Without positional encoding, the model cannot distinguish between a patch in the top-left corner and one in the bottom-right, making it impossible to learn spatial relationships like edges, shapes, or anatomical structures. The encoding is typically added directly to the patch embeddings before they enter the Transformer encoder, ensuring that the attention mechanism can factor in both what a patch contains and where it is located.
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
Positional encoding is the critical bridge between permutation-invariant self-attention and spatially-aware vision. Explore the key concepts that define how Transformers understand image structure.

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