Why positional encoding

AI from scratch · 15 of a series

Why positional encoding: lesson transcript

1. Two sentences, one bag

Attention scored every pair of tokens by their content alone, so these two sentences look identical to it: same five words, same keys, same queries, same weights. Tip the words into a bag and nothing distinguishes the sentence where the dog does the biting from the one where the man does. Order has to be put back in somehow.

2. Stamp each slot with a number?

The obvious fix: give every token one extra dial holding its slot number, 0, 1, 2, 3, 4. It works for short sentences and goes wrong fast. Slot 500 is a huge number that swamps the word dials, and a token has no easy way to ask "who is two slots before me?" from raw numbers. Position needs a representation with better shape.

3. Read position off a set of waves

Instead of one number, use a dozen dials, each a sine wave over position with its own wavelength: one that flips every couple of slots, one every few, one that barely changes across the whole sentence. Slide the position and read the height of each wave. Fast waves tell fine position, slow waves tell rough position, like the hands of a clock.

4. The fingerprint

Write every position's twelve readings as a row and stack the rows. This is the pattern: fast stripes on the left, slow bands on the right, and no two rows alike. Every slot has a unique fingerprint, the numbers stay between −1 and 1 forever, and the pattern keeps working at position 500 because the slow dials are still turning.

5. Add it to the word

Now simply add the fingerprint to the word's embedding before attention sees it. Dog in slot 1 and dog in slot 4 are the same word plus different fingerprints, so their keys and queries differ, and a query can be trained to prefer "a noun with a slot-1-ish fingerprint". The bag has become a sequence again.

6. Why waves and not something else

Here is the property that makes sines the clever choice. Dot the fingerprint of one slot with the fingerprint of another and the result depends only on how far apart they are, not on where they sit. So "two slots before me" is the same computation everywhere in the sentence, which is exactly what a query needs. Slide the anchor and watch the curve stay put.

7. Name it: positional encoding

The fingerprint is a positional encoding. This sine version was the original and needs no training. Later models let the fingerprints be learned dials, or rotate the keys and queries by an angle that grows with position, which bakes the "depends only on distance" property straight into the dot product. All of them answer the same question: where am I in the line?

8. Did it land?

Two fingerprints, some distance apart.

Check your understanding

What does the dot product of the fingerprints for positions 3 and 7 depend on?

Each dial pair is a sine and cosine of the same angle, and sin·sin + cos·cos of two angles collapses to the cosine of their difference. Every dial pair contributes a function of the gap alone, so the total is f(7 − 3) = f(4). That is why the similarity curve did not move when you slid the anchor.