Positional Encoding
A mechanism that informs the transformer about the order of tokens. The attention mechanism itself processes all tokens at once and does not distinguish between 'cat ate mouse' and 'mouse ate cat'.
How It Works
A vector, dependent on its position, is added to the embedding of each token. This vector is structured so that the model can calculate the distance between positions.
Types
- sinusoidal — fixed formulas, no training;
- learnable — positions receive their own vectors, which are refined during training;
- rotational (RoPE) — rotation of vectors by an angle dependent on position; used in modern LLM.
Why It Matters
Without positional encoding, the transformer would see a sentence as a 'bag of words'. It is the positions that turn a set of tokens into a meaningful sequence.