Attention treats a sentence as a bag of tokens — it has no built-in notion of which word came first. So the transformer adds a positional code to each token: a set of sine and cosine waves of different frequencies, PE(pos) = sin(pos/10000ᵢ), giving every position a unique, smooth fingerprint. The waves also let the model read relative distances by simple linear combinations.
The demo shows different positions get distinct sinusoidal codes (i13 sin via Taylor): live demo
“Transformers know word order.” — attention is permutation-equivariant; without positional encoding it cannot tell ‘dog bites man’ from ‘man bites dog.’ cited
Order, painted on as waves. architecture
On i-13, positions 0,1,2 get distinct codes (sin 0, 0.841, 0.909) — a deterministic per-position pattern: