Skip to main content

Positional Encoding Explained: Giving Transformers a Sense of Order

Understand how transformers track word order, compare sinusoidal, learned, relative, RoPE, and ALiBi encodings, and see a PyTorch implementation.
Aug 14, 2026  · 14 min read

Explore with AI

ChatGPTClaudePerplexity

Imagine trying to read a novel where every word on the page is scrambled into a random order. Even if you understand every single word, the plot is completely lost because the order of words dictates meaning. When engineers first began designing large language models (LLMs), they faced a similar hurdle. How do you teach a model to understand the sequence of words when its core mechanism processes everything simultaneously?

This is where positional encoding comes in. In this article, we will explore the inner workings of positional encoding and how they give transformers their sense of order. We will unpack its evolution from the original sinusoidal formulas to modern rotational embeddings and how to implement it in your next machine learning project. If you are new to the underlying architecture, I recommend starting with our guide on how transformers work.

Master Deep Learning in Python

Build in-demand deep learning skills through Python.
Start Learning for Free

What Is Positional Encoding?

Positional encoding is a technique for neural networks to understand the sequential order of input data. It acts as a set of coordinates that represents a token’s relative or absolute position and is merged with your standard data representation.

Before text goes into a neural network, words are converted into continuous numerical formats called embeddings. However, standard embeddings only capture semantic meaning: they do not tell you anything about the position of a word in a given text. You can learn exactly how this process works by taking our course Introduction to Embeddings with the OpenAI API.

A positional encoding is an additional mathematical layer added directly to these token embeddings. By combining the semantic representation with a positional stamp, the transformer model can distinguish between identical words that appear in different parts of a sentence.

The permutation invariance problem

Sequential data relies heavily on order. In natural language, the sentence "the dog bit the man" has a wildly different meaning than "the man bit the dog."

The permutation invariance problem in self-attention

Historically, Recurrent Neural Networks (RNNs) and Long Short-Term Memory networks (LSTMs) inherently handled this. They processed tokens sequentially, meaning the network mathematically ingested the second word only after finishing the first word. Similarly, Convolutional Neural Networks (CNNs) process data using local sliding windows that capture the spatial arrangement of pixels or neighboring words.

Transformers completely abandon sequential processing. They process all tokens in parallel. The core engine of a transformer is the self-attention mechanism, which calculates pairwise relationship scores between every single token in the sequence at the exact same time. Mathematically, self-attention is permutation invariant. If you shuffle the input sequence, the self-attention mechanism will output the exact same attention scores for the word pairs.

Without a mechanism to preserve order, the model experiences a total loss of sequential information. Semantic meaning collapses into an unordered bag of words. Positional encoding solves the permutation invariance problem by forcibly altering the input vectors so that the self-attention mechanism registers tokens at position 1 differently than tokens at position 10.

How Does Positional Encoding Work?

Positional encoding operates by mathematically modifying the input token vectors before they reach the self-attention layers. This ensures the model computes relationships based on both meaning and position.

Let’s build some intuition on what this means. Imagine standard token embeddings as name tags at a conference. The name tag tells you who the person is, but where they are in the conference doesn’t change who they are or their relations to other people. Positional encoding is like adding a seat number to that name tag. When you combine them, you know both who the person is and where they are sitting. This adds context to potential interactions this person might have at the conference.

In a transformer, this combination is typically achieved through simple element-wise addition. The model takes the semantic embedding vector of a word and adds a positional encoding vector of the exact same size. The resulting vector contains a subtle modulation that the subsequent layers learn to interpret as spatial coordinates.

Example calculation

Let us look at a simplified example. Imagine we have a tiny embedding space with a dimension of 4. We want to process the word "DataCamp" located at position 0 in our sequence.

  1. Semantic embedding: The neural network retrieves the embedding for "DataCamp", which might look like [0.51, -0.22, 0.88, 0.14].
  2. Positional encoding: The positional formula generates a coordinate vector for position 0. For this example, let us assume it calculates [0.00, 1.00, 0.00, 1.00].
  3. Combination: We add the two vectors together element-wise.
  4. Final input: The forward pass receives [0.51, 0.78, 0.88, 1.14].

Even if the exact same token "DataCamp" appears later at position 5, the positional encoding vector will be different. Consequently, the final input vector will look completely different to the self-attention mechanism.

Sinusoidal Positional Encoding

The original "Attention Is All You Need" paper introduced a brilliant method for generating positional vectors using basic trigonometry. This approach requires no machine learning weights and relies entirely on fixed mathematical functions.

Mathematical foundation

Sinusoidal positional encoding uses sine and cosine functions at different frequencies to encode positional information. The original formula dictates that for a given position (pos) and a specific dimension index (i) within the embedding vector, the encoding is calculated as follows:

  • For even dimensions (2i): PE(pos, 2i) = sin(pos / 10000^(2i/dmodel))

  • For odd dimensions (2i+1): PE(pos, 2i+1) = cos(pos / 10000^(2i/dmodel))

In this formula, dmodel is the total size of the embedding vector. As you move along the dimensions of the vector from index 0 up to dmodel, the wavelength of the sinusoidal functions geometrically increases.

Advantages and properties

This geometric progression of frequencies provides several distinct advantages. 

  1. The sine and cosine outputs are strictly bounded between -1 and 1. This bounded nature ensures that the positional encodings do not numerically overpower the semantic embeddings.
  2. The functions are periodic. Because of trigonometric addition formulas, a linear transformation can easily represent the positional encoding of a future position (pos + k) as a function of the current position (pos). This property naturally allows the model to learn relative position encoding.
  3. Sinusoidal encoding uses fixed parameters. It requires zero learnable weights, heavily reducing memory overhead. Because it operates on a continuous mathematical function, the model possesses a theoretical capability for extrapolation, meaning it can map positional coordinates for sequences longer than those seen during training.

Visualization

To help with understanding sinusoidal encoding, it can help to visualize it as a heatmap.

Sinusoidal positional encoding matrix

The x-axis of the visualization represents the embedding dimension size, while the y-axis indicates the token's position. 

The lower dimensions on the left oscillate rapidly between negative and positive values, which allows them to distinguish nearby token positions. As the dimension index increases, the wavelength becomes longer and the encoding changes more gradually. These dimensions capture positional differences over larger distances.

For the 50 positions shown here, many higher dimensions appear as nearly uniform vertical stripes. This happens because their wavelengths are so long that the sine values remain close to 0 and the cosine values remain close to 1 within this short sequence, not 0 and 1, respectively. So the alternating stripes reflect adjacent sine–cosine dimensions, not a simple binary encoding. Over a longer sequence, these dimensions would also begin to vary noticeably.

The takeaway is that the different frequencies give the model positional information at multiple scales: rapidly changing dimensions capture fine-grained differences between nearby tokens, while slowly changing dimensions track broader position within longer sequences.

Learned Positional Encoding

While fixed mathematical formulas are elegant, modern deep learning architectures often rely on data to learn optimal representations. Learned positional embeddings shift the burden of layout entirely to the training phase.

Concept and implementation

Instead of hardcoding the positional values with sine and cosine functions, learned positional embeddings treat the position coordinates as standard learnable parameters. 

The model initializes a completely blank embedding matrix where each row corresponds to a specific sequence position. During the standard backpropagation training loop, the network updates these positional weights alongside the semantic token embeddings to minimize the overall loss function.

Comparative advantages

The primary advantage of learned positional embeddings is extreme flexibility. It has the complete freedom to learn the exact positional representations that work best for a specific dataset. Architectures like BERT and GPT-2 successfully popularized this approach because of its high adaptability and strong performance on downstream tasks.

Limitations and challenges

The most significant limitation of learned positional embeddings is a hard boundary on sequence length. If a model is trained with a maximum of 2,048 learned position vectors, it physically cannot process a document of 2,049 tokens. 

This restriction severely limits length extrapolation and often requires costly retraining to extend the context window.

Relative Positional Encoding

As models evolved to handle longer documents, engineers realized that the absolute position of a word often matters less than its relative distance to other words.

Conceptual shift to relative positions

Consider the phrase "the rapid advancement of AI." The grammatical relationship between "advancement" and "AI" remains identical whether the phrase appears at the very beginning of a document or at the very end. Relative positional encoding abandons absolute grid coordinates. Instead, it focuses entirely on the distance offsets between interacting tokens.

Implementation in attention mechanisms

To implement relative encodings, engineers modify the attention score calculations directly rather than altering the input embeddings at the very beginning of the network. 

When computing the attention score between a "query" matrix and a "key" matrix, the operation includes an additional learned bias term representing the relative distance. This modification guarantees shift invariance within the text. For a deeper dive into how these matrices operate, read our guide on the attention mechanism in LLMs.

Practical trade-offs

This approach yields highly accurate contextual understanding but introduces severe computational overhead. Calculating unique relative distance matrices for every single attention head drastically increases memory consumption. It also complicates the internal caching mechanisms used to speed up text generation.

Absolute vs relative positional information

Deciding between absolute and relative encoding depends on the specific task. 

  • Absolute encoding works brilliantly when the exact spatial layout is rigid and fast execution is prioritized. 
  • Relative encoding proves far superior for long-form natural language processing, where the grammatical structure depends entirely on local context distances rather than absolute document coordinates.

Rotary Position Embedding (RoPE)

In the ongoing quest to perfect transformer architectures, researchers introduced Rotary Position Embedding (RoPE). It combines the best properties of absolute and relative positional encodings and has rapidly become an industry standard.

Innovative approach

RoPE unifies absolute placement and relative distances using rotational transformations. Instead of adding a vector to the embeddings or adding heavy biases to attention scores, RoPE maps the embedding vector into a complex plane and rotates it by a specific angle. The size of this angle is strictly determined by the token's absolute position in the sequence.

RoPE positional encoding

Mathematical insights

In a 2D space, the rotation is applied using a standard 2D rotation matrix. For higher dimensions, RoPE simply groups the embedding dimensions into pairs and applies unique 2D rotations to each chunk based on the absolute position index.

Because of the geometric properties of rotation matrices, the dot product of a rotated query vector and a rotated key vector ends up depending only on the relative angle between them. Thus, RoPE utilizes an absolute position mapping to naturally encode the relative distance between tokens.

RoPE vs sinusoidal encoding

As the graphic above shows, sinusoidal encoding changes the magnitude and direction of the original vector. RoPE instead preserves magnitude information and needs only the angle between two different positions to understand their relative distance.

Attention With Linear Biases (ALiBi)

While RoPE relies on complex rotational math, another technique called Attention with Linear Biases (ALiBi) takes a completely different approach.

Simplicity and efficiency

ALiBi completely discards the idea of adding positional information to the initial token embeddings. The word embeddings enter the transformer entirely untouched. 

Instead, ALiBi intervenes at the very last step of the attention calculation. Right before the attention logits pass through the softmax function, ALiBi subtracts a static penalty proportional to the distance between the two tokens. The further apart two words are, the larger the penalty subtracted from their attention score.

Performance

This method is quite simple to implement and very efficient. Its biggest benefit is extreme effectiveness at length-extrapolation tasks. A model trained with ALiBi on sequences of 1,024 tokens can easily generate coherent text on sequences double that length without crashing. 

For practitioners interested in exploring other advanced techniques utilized for fast text generation and inference tasks, see our guide on speculative decoding.

When to Use Which Positional Encoding

Start from what you're building, then pick the method that fits — sometimes it's one, sometimes several.

  • You want a simple, zero-parameter baseline: Sinusoidal (fixed formulas, nothing to train).
  • Your sequence length is fixed, and you want max performance on a specific dataset: Learned.
  • You need to handle longer sequences than those you trained on: RoPE is the strongest pick among the techniques that can extrapolate.
  • Grammar depends on the distance between tokens, not their absolute position: Relative or RoPE.
  • You're building a modern general-purpose LLM: RoPE.
  • Length extrapolation is your single top priority, and you want the simplest implementation: ALiBi.
  • You want relative-position benefits without the memory and caching overhead: RoPE (relative encodings give the benefit but are more expensive).

Practical Implementation and Architectural Considerations

Bridging the gap between theory and practice requires understanding how to integrate positional encodings into real codebases and manage architectural limitations.

Implementing positional encoding in transformers

To add positional encodings to a transformer in frameworks like PyTorch, developers typically create a dedicated module. This module generates the encoding matrix once and registers it as a buffer so it does not accidentally update during backpropagation. 

Below is a standard practical implementation of a forward pass incorporating sinusoidal encodings. The encodings are computed once from sine and cosine formulas and never change during training, which is why they're stored in a buffer rather than as learnable parameters (and why they add nothing to the training cost).

import torch
import torch.nn as nn
import math

class PositionalEncoding(nn.Module):
    def __init__(self, d_model: int, max_len: int = 5000):
        super().__init__()
        
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        
        # Calculate the division term for frequencies
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        
        # Apply sine to even indices and cosine to odd indices
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        
        pe = pe.unsqueeze(0)
        
        # Register as a buffer
        self.register_buffer('pe', pe)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # Add positional encoding directly to the input embeddings
        seq_len = x.size(1)
        x = x + self.pe[:, :seq_len, :]
        return x

In __init__, the module precomputes the encoding table up to max_len positions: position is a column of indices (0, 1, 2, …) and div_term is the frequencies, working out to 1 / 10000^(2i/d_model), so lower dimensions get fast waves and higher ones slow waves. 

Applying sin to even dimensions (0::2) and cos to odd (1::2) fills the table. Then forward does the only runtime work: it slices the table to the input's sequence length and adds it to the token embeddings element-wise. Note the module assumes a batch-first shape [batch, seq_len, d_model], which is why forward slices on dimension 1.

Position bias and the lost-in-the-middle phenomenon

An unintended consequence of mapping long sequence lengths is position bias. This frequently manifests as the "lost-in-the-middle" phenomenon. When processing massive documents, language models tend to remember facts from the very beginning or the very end of a prompt much better than those in between. This might seem familiar, since human memory works similarly (at least for me; don’t tell me I am the only one who remembers the beginning and end of books better then the rest!).

The theory is that this happens because the attention mechanism gets overwhelmed with irrelevant context and naturally anchors to the early tokens (which set the instruction) and the latest tokens (which are most recent in memory). To mitigate this bias, developers turn to advanced chunking strategies and semantic retrieval systems.

Positional encoding for specialized domains

Transformers are no longer restricted strictly to text. The concept of positional encoding adapts rapidly to handle different data types across multiple domains.

For vision transformers, images are chopped into grids of patches. A simple 1D sequence encoding fails to capture spatial proximity in 2D space. Therefore, vision models frequently use 2D learned positional encodings that account for both the X and Y coordinates of the image patch. 

Similar adaptations exist for continuous time series models, where specialized encodings track temporal distances, seasonality, and timestamps.

Further Research on Positional Encoding

As the race toward larger context windows accelerates, refining how models understand position remains a highly active and critical area of machine learning research.

Length extrapolation and context extension

Today's users demand massive context windows to process entire codebases or series of novels at once. Since training models from scratch on massive sequence lengths is too expensive, researchers rely on context extension techniques. 

Methods like positional interpolation mathematically compress the positional coordinates of a much longer sequence into the bounds of the original training length. Adaptive methods like YaRN dynamically adjust the rotational frequency of RoPE parameters to seamlessly stretch the model's understanding of distance.

Transformers without explicit positional encoding

Surprisingly, some research suggests explicit positional encoding might not always be strictly required. In decoder-only causal language models, the standard causal masking (a mechanism that forces tokens to only look backward) inherently leaks temporal ordering information. 

This means it might be possible to implicitly learn positional rules solely from the causal mask through strategic temperature tuning and architectural adjustments.

Challenges and Future Directions

The ultimate frontier highlights a persistent trade-off between strict adaptation (optimizing performance for a specific sequence length) and flawless extrapolation (allowing the model to generalize infinitely). 

Emerging research areas are heavily focused on multimodal encoding. Creating universal positional representations that merge 3D video coordinates with text and audio streams is one aim for the next generation of artificial intelligence models.

Conclusion

Positional encoding bridges the gap between parallel processing and sequential understanding and allows us to create models that can process large amounts of data in order. We explored how learned and relative embeddings offer extreme flexibility, and how modern innovations such as RoPE and ALiBi optimize both mathematical elegance and computational efficiency. 

As context windows extend from a few thousand tokens to millions, the simple mathematical act of telling a neural network exactly where things are located will remain at the very heart of model design. 

If you want to go deeper and get some hands-on experience, I recommend enrolling in our Developing Large Language Models skill track.

Positional Encoding FAQs

What is positional encoding in machine learning?

Positional encoding is a mathematical technique used to provide neural networks with information about the order of data in a sequence.

Why do transformer models need positional encoding?

Unlike older Recurrent Neural Networks that process data step by step, transformers process all data simultaneously using a self-attention mechanism. Because self-attention is naturally permutation invariant, it treats the input like an unordered bag of words. Positional encoding solves this by injecting sequential coordinates directly into the data before the model processes it.

What is the difference between absolute and relative positional encoding?

Absolute positional encoding assigns a fixed coordinate to a token based on its exact index in a sequence. Relative positional encoding ignores the exact grid location and instead computes the distance between two interacting tokens, focusing solely on their contextual offset.

What is Rotary Position Embedding (RoPE)?

RoPE is a widely used encoding method that maps token embeddings onto a complex plane and rotates them by an angle determined by their absolute position. When the model calculates attention scores, the relationship between two rotated vectors depends entirely on their relative distance.

What are learned positional embeddings?

Instead of using fixed mathematical formulas like sine and cosine, learned positional embeddings treat spatial coordinates as trainable weights. The model initializes a blank matrix of positions and updates these coordinates during training to learn optimal spatial representations for a specific dataset.


Tim Lu's photo
Author
Tim Lu
LinkedIn

I am a data scientist with experience in spatial analysis, machine learning, and data pipelines. I have worked with GCP, Hadoop, Hive, Snowflake, Airflow, and other data science/engineering processes.

Topics

Top Transformer Courses

Track

Developing Large Language Models

19 hr
Learn to develop large language models (LLMs) with PyTorch and Hugging Face, using the latest deep learning and NLP techniques.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

What Are Vector Embeddings? An Intuitive Explanation

Vector embeddings are numerical representations of words or phrases that capture their meanings and relationships, helping machine learning models understand text more effectively.
Tom Farnschläder's photo

Tom Farnschläder

9 min

blog

Attention Residuals Explained: Rethinking Transformer Depth

Learn how Attention Residuals rethink depth in Transformers by replacing uniform residual accumulation with selective, attention-based aggregation.
Aashi Dutt's photo

Aashi Dutt

8 min

blog

What is Text Embedding For AI? Transforming NLP with AI

Explore how text embeddings work, their evolution, key applications, and top models, providing essential insights for both aspiring & junior data practitioners.
Chisom Uma's photo

Chisom Uma

10 min

Tutorial

Variational Autoencoders: How They Work and Why They Matter

Learn the foundational principles, applications, and practical benefits of variational autoencoders and follow a step-by-step implementation with PyTorch.
Kurtis Pykes 's photo

Kurtis Pykes

Tutorial

Transformer Model Tutorial in PyTorch: From Theory to Code

Learn how to build a Transformer model using PyTorch, a powerful tool in modern machine learning.
Arjun Sarkar's photo

Arjun Sarkar

Tutorial

How Transformers Work: A Detailed Exploration of Transformer Architecture

Explore the architecture of Transformers, the models that have revolutionized data handling through self-attention mechanisms.
Josep Ferrer's photo

Josep Ferrer

See MoreSee More