Positional encoding tells a Transformer where each token sits in the sequence. Self-attention is permutation-invariant — it treats the input as a set, not a list — so without an explicit position signal, "dog bites man" and "man bites dog" look identical. The encoding injects order back in.
Where you meet it
- Every Transformer: GPT, BERT, T5, and the encoder–decoder of the original paper.
- Vision Transformers (ViT) — position over image patches instead of words.
- Long-context models, where the choice of encoding decides how far they can extrapolate.
What it does
It maps each position pos to a vector of the same width d as the token embeddings, then adds the two. After that, the token's representation carries both what it is and where it is — and attention finally has something to distinguish order by.
Attention sees a bag of words, not a sentence. Position isn't a detail you add — it's the only thing that makes order exist.
How it works
Attention computes its weights from dot products between tokens; nothing in that operation depends on index order. The fix is to give each position a distinct, smooth fingerprint. The original Transformer uses fixed sinusoids of different frequencies — one sine/cosine pair per embedding dimension:
PE(pos, 2i) = sin(pos / 10000^(2i/d))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d))
Low dimensions oscillate fast, high dimensions slowly — like the hands of a clock, together they pin down a position uniquely and continuously. Because each frequency is a sinusoid, PE(pos+k) is a fixed linear function of PE(pos), so relative distances stay recoverable by the model. Two common alternatives: learned position embeddings (a trainable vector per slot, as in BERT) and RoPE (rotary), which rotates the query and key vectors by a position-dependent angle so relative position falls out of the attention dot product directly.
Watch out
- Without any positional signal, attention is order-blind — the "dog bites man" / "man bites dog" collapse is the whole reason this exists.
- It's added, not concatenated: the position vector lives in the same space as the embedding and overlaps it, rather than taking up extra dimensions.
- Absolute vs relative vs RoPE matters: absolute encodes "slot 7", relative and RoPE encode "3 tokens apart" — usually better for generalising across lengths.
- Extrapolation beyond the training length is hard. Learned embeddings simply have no vector for unseen positions; sinusoids and RoPE degrade more gracefully but still need tricks (interpolation, NTK scaling) for very long contexts.
Go deeper
Positional Encoding sagt einem Transformer, an welcher Stelle der Sequenz jedes Token steht. Self-Attention ist permutationsinvariant — sie behandelt die Eingabe als Menge, nicht als Liste. Ohne explizites Positionssignal sähen "Hund beißt Mann" und "Mann beißt Hund" gleich aus. Das Encoding bringt die Reihenfolge zurück.
Wo es vorkommt
- In jedem Transformer: GPT, BERT, T5 und der Encoder-Decoder des Originalpapers.
- Vision Transformer (ViT) — Position über Bild-Patches statt über Wörter.
- Long-Context-Modelle, wo die Wahl des Encodings entscheidet, wie weit sie extrapolieren.
Was es tut
Es bildet jede Position pos auf einen Vektor derselben Breite d wie die Token-Embeddings ab und addiert beide. Danach trägt die Token-Repräsentation sowohl was sie ist als auch wo sie steht — und Attention hat endlich ein Merkmal, an dem sie Reihenfolge unterscheiden kann.
Attention sieht einen Beutel voller Wörter, keinen Satz. Position ist kein Detail — sie lässt Reihenfolge überhaupt erst existieren.
Wie es funktioniert
Attention berechnet ihre Gewichte aus Skalarprodukten zwischen Token; nichts daran hängt von der Index-Reihenfolge ab. Die Lösung: jeder Position einen eindeutigen, glatten Fingerabdruck geben. Der Original-Transformer nutzt feste Sinusoide verschiedener Frequenzen — ein Sinus-/Kosinus-Paar pro Embedding-Dimension:
PE(pos, 2i) = sin(pos / 10000^(2i/d))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d))
Niedrige Dimensionen schwingen schnell, hohe langsam — wie Zeiger einer Uhr legen sie zusammen eine Position eindeutig und stetig fest. Da jede Frequenz ein Sinusoid ist, ist PE(pos+k) eine feste lineare Funktion von PE(pos), sodass das Modell relative Abstände rekonstruieren kann. Zwei gängige Alternativen: gelernte Positions-Embeddings (ein trainierbarer Vektor pro Slot, wie in BERT) und RoPE (Rotary), das Query- und Key-Vektoren um einen positionsabhängigen Winkel rotiert, sodass die relative Position direkt aus dem Attention-Skalarprodukt fällt.
Worauf achten
- Ohne Positionssignal ist Attention reihenfolgenblind — der Kollaps "Hund beißt Mann" / "Mann beißt Hund" ist der ganze Grund, warum es das gibt.
- Es wird addiert, nicht konkateniert: der Positionsvektor lebt im selben Raum wie das Embedding und überlagert es, statt extra Dimensionen zu belegen.
- Absolut vs. relativ vs. RoPE macht einen Unterschied: absolut kodiert "Slot 7", relativ und RoPE kodieren "3 Token Abstand" — meist besser, um über Längen hinweg zu generalisieren.
- Extrapolation über die Trainingslänge hinaus ist schwierig. Gelernte Embeddings haben schlicht keinen Vektor für ungesehene Positionen; Sinusoide und RoPE verschlechtern sich sanfter, brauchen aber für sehr lange Kontexte Tricks (Interpolation, NTK-Skalierung).