Positional Encoding

Two-panel comic: 'The Problem' shows two robot newsboys with 'DOG BITES MAN' and 'MAN BITES DOG' headlines while a confused editor sees the same jumbled words; 'The Fix' shows a clock machine with fast/medium/slow hands stamping unique wave patterns onto word cards numbered #1, #2, #3, so order now matters
What You’ll Learn

You’ll learn why self-attention is position-blind and needs an added signal to tell “dog bites man” from “man bites dog.” You’ll see how sinusoidal encodings use waves, why nearby positions stay similar, and how RoPE puts relative position directly into attention.

The Problem: Attention Doesn’t Know Order

Consider “Dog bites man” (routine news) versus “Man bites dog” (breaking news!). These sentences have completely different meanings, but raw self-attention computes the same attention weights for both. Play the 3 rounds below to see why:

Round 1 · Test “Dog → bites” Click the cell where Query “Dog” attends to Key “bites” in either matrix.
Sentence A: “Dog bites man”
Key →
Query ↓
Dog
bites
man
Dog
bites
man
Sentence B: “Man bites dog”
Key →
Query ↓
Man
bites
dog
Man
bites
dog

RNNs don’t have this problem because they process tokens one at a time, left to right. Position is baked into the computation order. Transformers process all positions in parallel, which makes them fast but position-blind until we stamp position vectors onto each token.

Fill in the Blanks
Self-attention is -invariant: shuffling the input tokens produces the same attention weights.
Fill in every blank, then check your answer.

The Fix: Add Position Vectors

The idea: create a positional encoding vector for each position and add it to the token embedding before attention. Now “cat” at position 1 has a different representation than “cat” at position 0, even though they are the same word. Step through the animation to see how:

d0
d1
d2
d3
d4
d5
d6
d7
"The"
 0.20
-0.50
 0.80
 0.10
-0.30
 0.70
-0.10
 0.40
"cat"
-0.30
 0.60
 0.20
-0.70
 0.40
 0.10
 0.50
-0.20
"sat"
 0.10
-0.20
 0.50
 0.30
-0.60
 0.40
 0.20
 0.80
Each token has an embedding vector that encodes WHAT the token is. But notice: if "cat" appeared at position 0 instead of position 1, its embedding would be identical. The embedding carries no information about WHERE the token sits in the sentence.

What to look for: in Step 3, the final vectors (green) differ between tokens that would otherwise be identical. The position “nudge” breaks the symmetry that self-attention can’t see on its own.


How Sinusoidal Encoding Works

The original Transformer (2017) builds position vectors from sine and cosine waves at different frequencies. One wave isn’t enough to distinguish every position (it repeats). But layering many waves, fast and slow, creates a unique fingerprint for each one. Step through this buildup:

Building a Positional Fingerprint

10-102468101214Position
dim 0 (fast)
Position fingerprints (values from active waves):
pos 0
0.00
pos 1
0.84
pos 2
0.91
pos 3
0.14
pos 4
-0.76
pos 5
-0.96
pos 6
-0.28
pos 7
0.66
A single sine wave gives each position a value. But notice: positions 0 and 6 have similar values (the wave repeats). One wave alone can't uniquely identify every position.

What to look for: with one wave, positions 0 and 6 look similar. Adding a second wave separates them. By the final step, every position has a completely unique combination of values across all dimensions.

Think of it like the hands on a clock. The second hand spins fast (low dimensions), the minute hand spins at medium speed (middle dimensions), and the hour hand barely moves (high dimensions). Together they uniquely identify any moment in time. Where this analogy breaks: unlike a clock, PE uses sine and cosine at each frequency, giving it two values per frequency rather than one.


Explore the Full Encoding

Switch between heatmap view (all dimensions at once) and wave view (individual dimensions across positions):

Positional Encoding Visualizer

to
to
Position
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Dimension
-1
+1
PE(pos, 2i) = sin(pos / 100002i/d)     PE(pos, 2i+1) = cos(pos / 100002i/d)

What to notice:

  • Low dimensions (left columns) change rapidly, high-frequency “second hand”
  • High dimensions (right columns) change slowly, low-frequency “hour hand”
  • Each position has a unique combination of values across all dimensions

Nearby Positions Have Similar Encodings

A useful property of sinusoidal encoding: similarity between positions depends on distance, not absolute values. Hover over the matrix below to see cosine similarity between position vectors:

PE Cosine Similarity Matrix

Hover cells to see similarity values
pos
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0.55 (least similar)
1.00 (most similar)
Key observation: The diagonal stripe pattern shows that similarity depends mainly on relative distance between positions, not absolute position. Nearby positions are similar; distant ones are less so.

What to look for: the bright diagonal band shows that nearby positions have high similarity, while distant positions are dissimilar. Notice the pattern is symmetric and repeats: the similarity between positions 2 and 5 is the same as between positions 10 and 13.

This means the model can learn patterns like “the word 2 positions to my left is usually a verb,” and that pattern works at position 5 the same as at position 500. No relearning needed for each absolute position.

Quick Check
Can sinusoidal positional encoding handle sequences longer than those seen during training?
Your turn
Choose an answer to check your understanding.


Quick Summary

ConceptWhat HappensIntuition
Position-blind attentionSelf-attention produces identical outputs regardless of word orderIt sees a set of tokens, not a sequence: “dog bites man” = “man bites dog”
Positional encodingA position vector is added to each token embedding before attentionLike stamping a seat number on each concert ticket: same person, different seat
Sinusoidal encodingEach dimension uses a sine/cosine wave at a different frequencyLike clock hands: seconds change fast (low dims), hours change slowly (high dims)
Nearby similarityPositions close together have similar encoding vectorsThe model can learn relative patterns like “the word 2 positions left is usually a verb”
Fill in the Blanks
Sinusoidal positional encoding uses different waves to create unique position signatures, where low dimensions change and high dimensions change slowly.
Fill in every blank, then check your answer.

Learning Checklist
  • I can explain why self-attention is position-blind and give a concrete example where word order changes meaning
  • I understand that positional encoding is added to the token embedding, and can explain why addition works
  • I can describe the clock analogy: low dimensions = fast-changing second hand, high dimensions = slow-changing hour hand
  • I understand why nearby positions have similar encodings and why that helps the model learn
  • I know that sinusoidal PE can extrapolate to unseen sequence lengths because sine/cosine are defined for all positions

Next: Where Attention Came From

You now understand how modern transformers use attention: self-attention, multiple heads, and positional encoding. But where did the idea of “attention” originate? Before transformers, it was invented to help translation models focus on relevant parts of the input sentence. Seq2Seq Attention →