Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

How Transformers Grasp Word Order: A Deep Dive into Positional Encoding Methods

PhyLosophy 2 min read

Transformers, the architecture behind modern large language models (LLMs), have a fundamental limitation: without additional mechanisms, they treat sentences as unordered bags of words. This is because the self-attention mechanism is permutation equivariant — it has no inherent sense of sequence order. To address this, positional encodings are added to give the model information about word positions.

There are several approaches to positional encoding:

Sinusoidal Positional Encoding

Introduced in the original Transformer paper, this method uses fixed sine and cosine functions of different frequencies to encode positions. Each position is mapped to a vector where each dimension corresponds to a sinusoid. The key advantage is that it requires no learned parameters and can extrapolate to sequences longer than those seen during training.

Learned Positional Encoding

Instead of fixed functions, learned embeddings are trained alongside the model. Each position has its own trainable vector, allowing the model to adapt the representation to the task. However, this method cannot handle sequence lengths beyond those seen during training.

Rotary Position Embedding (RoPE)

RoPE has become the standard in modern LLMs. It encodes position by rotating the query and key vectors in attention, effectively applying a rotation matrix that depends on the position difference. This directly captures relative positions, allowing the model to generalize to longer sequences and better handle relative order.

In summary, positional encoding is crucial for Transformers to understand word order. Sinusoidal methods are fixed and extrapolatable, learned embeddings are flexible but limited, and RoPE combines the benefits of both — it is relative, efficient, and widely adopted in today's state-of-the-art models.

React