Introduction
Positional encodings are mathematical representations added to token embeddings in neural network architectures to convey sequential or spatial information. They are a foundational component of Transformer models, which lack inherent sequential processing mechanisms like recurrence or convolution.
Without positional information, self-attention mechanisms would treat input sequences as unordered sets, fundamentally limiting their ability to model language, time series, or spatial data. Positional encodings resolve this by injecting order-aware signals directly into the embedding space.
Why Positional Information Matters
The core attention operation computes relationships between tokens based solely on their content. Mathematically, the attention weights are permutation-invariant:
Attention(Q, K, V) = softmax(QKT/√dk)V
Reordering the input sequence does not change the output distribution if positions are unencoded.
To restore sequential awareness, positional signals must be integrated. This can be achieved through:
- Fixed functions (e.g., sinusoidal encodings)
- Learned parameters (trainable position embeddings)
- Relative/bias mechanisms (incorporating position differences during attention)
Sinusoidal Positional Encodings
Introduced in the original Attention Is All You Need paper (Vaswani et al., 2017), sinusoidal encodings use fixed sine and cosine functions of varying frequencies to represent positions:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
Key properties:
- Each dimension corresponds to a sinusoid of a different wavelength
- Enables the model to learn to attend to relative positions easily
- Generalizes to sequence lengths longer than those seen during training
- PE(pos + k) can be represented as a linear function of PE(pos)
Learned Positional Encodings
Alternative approaches treat positions as trainable embeddings, similar to word embeddings. A lookup table of size max_seq_length × d_model is initialized randomly and updated during backpropagation.
Advantages: Simpler to implement; adapts to dataset-specific distributional patterns.
Limitations: Struggles with extrapolation beyond training sequence lengths; requires fixed maximum context windows.
Modern Variants & Advances
Rotary Positional Embeddings (RoPE)
Proposed by Su et al. (2021), RoPE encodes positional information through rotation matrices applied to query and key vectors. It naturally captures relative positional information without modifying the attention mechanism itself, and has become a standard in LLMs like LLaMA and PaLM.
ALiBi (Attention with Linear Biases)
ALiBi adds a linear penalty to attention scores proportional to the distance between tokens, eliminating explicit position embeddings entirely. This approach demonstrates exceptional length extrapolation capabilities and is used in models like Bloom.
T5 Absolute Positional Encodings
Google's T5 architecture uses learned absolute embeddings but trains on diverse tasks to encourage robustness. It pairs well with relative attention biases in later refinements.
Implementation Example
import torch
import torch.nn as nn
import math
class SinusoidalPositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=5000):
super().__init__()
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2, dtype=torch.float) *
-(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0) # Shape: (1, max_len, d_model)
self.register_buffer('pe', pe)
def forward(self, x):
# x: (batch_size, seq_len, d_model)
return x + self.pe[:, :x.size(1), :]
Impact & Limitations
Positional encodings bridge the gap between permutation-invariant attention and sequential data. However, they face challenges:
- Length extrapolation: Fixed embeddings struggle beyond training horizon
- Modality transfer: Text-optimized encodings don't always generalize to images or audio
- Interference: Position signals can entangle with semantic embeddings, affecting representation geometry
Ongoing research focuses on dynamic, adaptive, and modality-agnostic position representation schemes, including Fourier-based encodings, length-adaptive rotations, and diffusion-aware positioning.