Start studying
Module 5 · Transformers Module contents
Module 5  ·  Card 8 / 19beginner

Positional Encoding

Self-attention has no built-in sense of order.

Without help it would treat a sentence as an unordered bag of tokens.

Positional encoding injects information about each token's position.

The original paper used fixed sine and cosine waves of different frequencies.

Modern models often use learned or rotary positional embeddings instead.

Learn in depth ↓
Module 05 · Transformers · 19 concepts
In depth  /  15 lines

Positional Encoding, in more detail

Attention treats its input as a set, not a sequence.

Every position is compared with every other in the same way, with no notion of order.

Shuffle the words and the raw mechanism computes the same thing.

That is fatal for language, where the dog bit the man is not the man bit the dog.

Position therefore has to be added to the token vectors explicitly.

The original transformer added fixed patterns of sine and cosine waves.

Each dimension uses a different frequency, so every position gets a distinct signature.

Fixed waves need no training and extend in principle to unseen lengths.

Learned positional embeddings are the alternative, with one vector per position.

They fit the data better and stop working past the longest length seen in training.

That limit is one reason context windows were historically hard to extend.

Both approaches encode absolute position, which is a slightly odd thing to care about.

What usually matters is relative distance, since two words being adjacent is the useful fact.

Rotary embeddings encode position as a rotation and give relative distance directly.

That is why nearly every recent model uses them instead.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Positional Encoding

Why anything is needed

Self-attention is permutation-equivariant: shuffle the input and the outputs shuffle with it, unchanged.

So order has to be added to the representation itself, before attention ever sees it.

xp′=xp+PE(p)x'_p = x_p + \mathrm{PE}(p)
  • pp: the position index.
  • PE(p)\mathrm{PE}(p): a vector of the same width as the embedding.

It is added, not concatenated. That keeps the width fixed and lets the model decide how much of each dimension to spend on position.

The sinusoidal scheme

PE(p,2i)=sin⁡ ⁣(p100002i/d)PE(p,2i+1)=cos⁡ ⁣(p100002i/d)\mathrm{PE}(p, 2i) = \sin\!\left( \frac{p}{10000^{2i/d}} \right) \qquad \mathrm{PE}(p, 2i+1) = \cos\!\left( \frac{p}{10000^{2i/d}} \right)
  • ii: which pair of dimensions, from 0 to d/2d/2.
  • dd: the model width.

Each pair of dimensions is a sine and cosine of the same frequency, and the frequency falls geometrically across the pairs.

The fastest pair alternates every couple of positions. The slowest has a wavelength of about 60,000, longer than any sequence it will see.

Together they encode position the way a binary counter does, with each dimension flipping at a different rate.

Why sinusoids rather than just the index

Feeding pp in raw would be unbounded and would dominate the embedding at long positions.

The real argument is that a fixed offset becomes a linear transformation.

PE(p+k)=Rk PE(p)\mathrm{PE}(p + k) = R_k \, \mathrm{PE}(p)
  • RkR_k: a rotation matrix depending only on kk, not on pp.

That follows from the angle-addition identities, and it means the model can learn to attend "three positions back" as one linear operation that works everywhere in the sequence.

It also extrapolates in principle: the function is defined for any pp, including positions longer than anything in training. In practice that extrapolation degrades quickly.

One forward pass, in order

  1. Look up the token embedding for each position.
  2. Compute or look up the positional vector for that index.
  3. Add the two together.
  4. Pass the sum into the first attention layer.
  5. Do this once at the bottom, not at every layer.

What replaced it

Learned absolute embeddings simply train a vector per position, which works as well within the training length and not at all beyond it.

Rotary embeddings rotate the queries and keys instead of adding to the input, which makes the dot product depend on relative position directly.

That last property is why almost every recent model uses rotary rather than sinusoidal, and why sinusoidal is now mostly of historical interest.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What would a transformer do without any positional encoding?
Transformers

19 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close