Positional Encoding.
A technique that adds position information to tokens in transformer models, enabling them to understand word order despite attention being inherently position-agnostic.
Positional encoding is a fundamental component of transformer architectures that solves a critical limitation: the attention mechanism treats all tokens as an unordered set, with no inherent understanding of sequence order. Without positional encoding, a transformer would process "The cat sat on the mat" identically to "Mat the on sat cat the," making it impossible to understand language structure. This technique injects position information directly into the token representations, allowing models to distinguish between tokens based on both their content and their location in the sequence.
There are two primary approaches to positional encoding: sinusoidal and learned encodings. Sinusoidal encodings use fixed mathematical functions (sine and cosine waves of different frequencies) to create unique position signatures that can theoretically handle sequences of any length and allow models to extrapolate to longer sequences than seen during training. Learned positional encodings, in contrast, treat position information as trainable parameters that the model optimizes during training, potentially capturing more complex positional relationships but being limited to the maximum sequence length encountered during training. Some modern approaches combine both methods or use relative positional encodings that focus on the distance between tokens rather than absolute positions.
The choice of positional encoding significantly impacts model performance and capabilities, particularly for tasks requiring strong understanding of word order like syntax parsing or code generation. While sinusoidal encodings offer better length generalization, learned encodings often perform better on tasks where the model has seen sufficient training data at the target sequence lengths. A common misconception is that positional encoding is only added once at the input layer, but the position information must be preserved and utilized throughout all transformer layers to maintain order awareness during the complex attention computations that follow.