Chapter 4: Positional Encoding (Teaching Word Order)
Chapter 4: Positional Encoding (Teaching Word Order)
Part 1: Understanding the Core Problem
The Parallelism Discovery
Let's start with a fundamental realization about how transformers work differently from how we humans read.
How humans read (sequential):
Step 1: Read "I" → understand it
Step 2: Read "love" → understand it
Step 3: Read "pizza" → understand it
Time: 3 steps (one after another)
We process words one at a time, in order. This is called sequential processing.
How transformers read (parallel):
Step 1: Look at ALL words simultaneously → understand all at once
Time: 1 step (everything together)
The transformer sees all three words at the same instant! This is called parallel processing, and it's WHY transformers are so fast! 🚀
But this speed creates a massive problem...
The Problem: Order Blindness
Remember from Chapter 3, we converted our sentence to embeddings. Important reminder: these numbers are currently RANDOM! We initialized them randomly, and they don't mean anything yet. After training, they'll learn to represent word meanings, but right now they're just random starting points.
"I": [0.01, -0.20, 0.30, 0.15, -0.05, 0.11] ← Random numbers (for now)
"love": [-0.40, 0.60, 0.00, 0.25, 0.90, -0.30] ← Random numbers (for now)
"pizza": [0.90, -0.10, -0.20, -0.80, 0.50, 0.75] ← Random numbers (for now)
Now here's the problem. These are just three vectors—three lists of numbers. They could be in ANY order, and the transformer wouldn't know the difference!
The Bag of Marbles Analogy:
Imagine you have a bag with three colored marbles:
- 🔴 Red marble
- 🔵 Blue marble
- 🟢 Green marble
You shake the bag and pour them out onto a table. They might come out:
- Green, red, blue
- Or blue, green, red
- Or red, blue, green
The bag doesn't remember their original order! Once they're on the table, they're just three marbles with no inherent sequence.
Same with our embeddings! The transformer sees:
Vector #1: [0.01, -0.20, 0.30, 0.15, -0.05, 0.11]
Vector #2: [-0.40, 0.60, 0.00, 0.25, 0.90, -0.30]
Vector #3: [0.90, -0.10, -0.20, -0.80, 0.50, 0.75]
Which is first? Which is last? There's nothing in these numbers that says "I'm the first word" or "I'm the third word." They're just three piles of numbers floating in space!
Why Order Matters: The Meaning Crisis
Let's see why this is catastrophic with real examples:
Example 1: Who did what to whom?
- "The dog bit the man" 🐕→👨 (dog did the biting)
- "The man bit the dog" 👨→🐕 (man did the biting!)
Same exact words, but completely opposite meanings! Order is EVERYTHING.
Example 2: Making sense vs nonsense
- "I love pizza" ✓ (normal, happy sentence)
- "Pizza love I" ✗ (sounds like Yoda having a stroke)
- "Love I pizza" ✗ (complete word salad)
Example 3: Warnings and commands
- "Don't eat that!" 🚫🍴 (urgent warning!)
- "Eat that, don't!" ✗ (confusing and weird)
- "That don't eat!" ✗ (makes no sense at all)
Example 4: Questions vs statements
- "You are going?" (asking a question)
- "Are you going?" (also a question, but different emphasis)
- "Going you are?" ✗ (gibberish)
Without position information, the transformer would think all of these are the same because they contain the same words! 😱
What We Need: The Requirements
Before we jump into solutions, let's think about what the IDEAL position encoding needs:
Requirement 1: Each position must be unique
- Position 1 must look different from position 2
- Position 2 must look different from position 3
- No two positions should ever have the same "fingerprint"
Requirement 2: Stay bounded (don't get too big!)
- Our embeddings are small numbers (between -1 and 1)
- Position info shouldn't overwhelm word meaning
- Must stay in a reasonable range even at position 1000
Requirement 3: Work for ANY sentence length
- Short sentences (5 words)
- Medium sentences (50 words)
- Long documents (1000+ words)
- Even lengths we've never seen before!
Requirement 4: Consistent distances
- "Next to each other" should always mean the same thing
- Whether it's words 5 and 6, or words 100 and 101
- The model needs to learn consistent patterns
Requirement 5: Multi-scale information
- Distinguish nearby words (position 1 vs 2)
- Also distinguish distant words (position 1 vs 100)
- Both local and global position info
Can we build something that satisfies ALL these requirements? Let's try!
Part 2: Failed Attempts (Learning from Mistakes)
Before we jump to the solution, let's try the "obvious" approaches and understand WHY they fail. This will help us appreciate the elegant final solution!
Attempt #1: Just Use Position Numbers (1, 2, 3...)
"Why not just add the position number to each embedding?"
Good intuition! The idea is simple: just label each word with its position. Let's try it with a longer sentence to really see what happens: "I really love eating delicious homemade pizza with my best friends"
Setting up the addition:
Position 0: "I" → [0.01, -0.20, 0.30, 0.15, -0.05, 0.11] + [0, 0, 0, 0, 0, 0]
Position 1: "really" → [0.34, 0.12, -0.45, 0.22, 0.67, -0.18] + [1, 1, 1, 1, 1, 1]
Position 2: "love" → [-0.40, 0.60, 0.00, 0.25, 0.90, -0.30] + [2, 2, 2, 2, 2, 2]
Position 3: "eating" → [0.55, -0.33, 0.21, -0.12, 0.44, 0.08] + [3, 3, 3, 3, 3, 3]
Position 4: "delicious" → [0.78, 0.05, -0.66, 0.31, -0.22, 0.49] + [4, 4, 4, 4, 4, 4]
Position 5: "homemade" → [-0.11, 0.88, 0.13, -0.55, 0.37, -0.71] + [5, 5, 5, 5, 5, 5]
Position 6: "pizza" → [0.90, -0.10, -0.20, -0.80, 0.50, 0.75] + [6, 6, 6, 6, 6, 6]
Position 7: "with" → [0.02, -0.47, 0.61, 0.18, -0.29, 0.33] + [7, 7, 7, 7, 7, 7]
Position 8: "my" → [-0.58, 0.24, 0.09, -0.41, 0.73, -0.15] + [8, 8, 8, 8, 8, 8]
Position 9: "best" → [0.41, -0.69, 0.52, 0.07, -0.35, 0.88] + [9, 9, 9, 9, 9, 9]
Position 10: "friends" → [0.90, -0.10, -0.20, -0.80, 0.50, 0.75] + [10, 10, 10, 10, 10, 10]
After doing the addition:
Position 0: "I" → [0.01, -0.20, 0.30, 0.15, -0.05, 0.11]
Position 1: "really" → [1.34, 1.12, 0.55, 1.22, 1.67, 0.82]
Position 2: "love" → [1.60, 2.60, 2.00, 2.25, 2.90, 1.70]
Position 3: "eating" → [3.55, 2.67, 3.21, 2.88, 3.44, 3.08]
Position 4: "delicious" → [4.78, 4.05, 3.34, 4.31, 3.78, 4.49]
Position 5: "homemade" → [4.89, 5.88, 5.13, 4.45, 5.37, 4.29]
Position 6: "pizza" → [6.90, 5.90, 5.80, 5.20, 6.50, 6.75]
Position 7: "with" → [7.02, 6.53, 7.61, 7.18, 6.71, 7.33]
Position 8: "my" → [7.42, 8.24, 8.09, 7.59, 8.73, 7.85]
Position 9: "best" → [9.41, 8.31, 9.52, 9.07, 8.65, 9.88]
Position 10: "friends" → [10.90, 9.90, 9.80, 9.20, 10.50, 10.75]
Do you see the problem emerging? The original embeddings (small numbers like 0.90, -0.10) are getting completely overwhelmed by the position numbers (10, 9.90, etc.)! Let's understand WHY this is catastrophic...
Problem #1: What Each Dimension Will Learn (Understanding the Real Issue)
Before we talk about why big numbers are bad, let's understand what's SUPPOSED to happen after training.
Remember: Right now, these embedding numbers are RANDOM. After training, each dimension will learn to represent BOTH word meaning AND position information combined!
For example, after training is complete:
-
Dimension 0 might learn to represent: "Is this word about food?" + "Position marker 0"
- For "pizza" at position 6: maybe 0.85 (food=yes) + 6 (position) = 6.85
- For "with" at position 7: maybe 0.02 (food=no) + 7 (position) = 7.02
-
Dimension 1 might learn to represent: "Is this word a verb?" + "Position marker 1"
- For "love" at position 2: maybe 0.90 (verb=yes) + 2 (position) = 2.90
- For "pizza" at position 6: maybe -0.10 (verb=no) + 6 (position) = 5.90
-
Dimension 2 might learn to represent: "Emotion level?" + "Position marker 2"
- And so on...
This is the KEY insight: Each dimension will eventually encode BOTH a semantic meaning (what the word represents) AND position information. The model needs to learn patterns in BOTH parts of each number!
Now here's the catastrophic problem with using 1, 2, 3...
Problem #1a: The "Shouting Problem" - Scale Mismatch
Look at position 10 from our example. The word "friends" has:
Original embedding: [0.90, -0.10, -0.20, -0.80, 0.50, 0.75]
Add position 10: [10, 10, 10, 10, 10, 10 ]
───── ───── ───── ───── ──── ──────
Result: [10.90, 9.90, 9.80, 9.20, 10.50, 10.75]
Let's think about what dimension 0 is trying to learn:
- It WANTS to learn: "Is this word about food?" (small signal, like 0.90)
- It's FORCED to also encode: "This is position 10" (huge signal, 10)
- Combined value: 10.90
The model sees 10.90 and needs to figure out:
- "Is the word meaning 0.90 and position 10?"
- "Or is the word meaning 10.50 and position 0.40?"
- "Or maybe word meaning 5.45 and position 5.45?"
The huge position number (10) dominates the signal! It's like trying to hear a whisper (0.90) next to a jet engine (10). The model will spend 99% of its attention learning "this is position 10" and barely notice the tiny word meaning buried in there.
Problem #1b: Even Derivatives Can't Save Us (The Real Technical Problem)
You might be thinking: "Wait! Can't we use calculus/derivatives to separate them during training? During backpropagation, we compute gradients, and we can separate the position part from the word meaning part, right?"
Great question! This is exactly what a thoughtful beginner would ask. Here's why it STILL doesn't work:
Yes, mathematically we CAN separate them using derivatives:
Combined = WordMeaning + PositionNumber
∂Combined/∂WordMeaning = 1 (gradient flows through)
So technically, the gradient tells us "how to update the word meaning part." But there are THREE massive problems:
Problem A: Learning Rate Mismatch
During training, we update parameters using:
new_value = old_value - learning_rate × gradient
Typical learning rate: 0.0001 (very small!)
For dimension 0 at position 1000:
Current value: 1000.2 (= word meaning 0.2 + position 1000)
Gradient says: "Increase word meaning by 0.5"
Update: 1000.2 - 0.0001 × (-0.5) = 1000.20005
The update is SO TINY compared to the huge number (1000.2) that learning becomes glacially slow! The model needs to make a 0.5 change to the meaning part, but because it's buried in 1000.2, the 0.0001 learning rate barely budges it!
Problem B: Numerical Precision Loss
Computers store numbers with limited precision (typically 32-bit floats have ~7 significant digits).
Position 1000, word meaning 0.234567:
Exact value: 1000.234567
Stored as: 1000.2346 (lost precision on the last digits!)
When the position number is 1000, the tiny changes to word meaning (in the 0.001 range) get lost in rounding errors! The computer literally can't represent the precise changes needed for learning the meaning part.
Problem C: Network Capacity Waste
The transformer needs to learn: "Dimension 0 values around 1000 mean position ~1000, and I need to look at the tiny decimal part for word meaning."
This is like needing a SEPARATE mental model for each position range:
- Position 0-10: "Look at numbers 0-11, meaning is in the decimal"
- Position 100-110: "Look at numbers 100-111, meaning is in the decimal"
- Position 1000-1010: "Look at numbers 1000-1011, meaning is in the decimal"
The model would need to learn 1000 different "scales" to handle different position ranges! This wastes enormous network capacity that should be learning about language patterns, not about "how to read decimals at different scales."
In essence: Even though we CAN mathematically separate them with derivatives, the practical learning process becomes impossibly inefficient. It's like trying to measure the weight of a feather while it's sitting on an elephant—yes, the total weight changed, but good luck detecting and learning from that tiny difference!
Problem #1c: Visualization of the Disaster
Let's see this with a longer document at position 1000:
Word "amazing" at position 1000:
Word embedding wants to learn: [0.75, -0.30, 0.45, 0.60, -0.20, 0.35]
↑ ↑ ↑ ↑ ↑ ↑
"positive" "noun" "intensity" etc...
After adding position 1000: [1000.75, 999.70, 1000.45, 1000.60, 999.80, 1000.35]
↑ ↑ ↑ ↑ ↑ ↑
Position is screaming! Word meaning is buried!
The transformer sees all dimensions around 1000 and learns: "Oh, this is position ~1000!"
But it struggles to learn the subtle patterns in the decimals (0.75 vs 0.70 vs 0.45) that encode the actual word meaning. The signal-to-noise ratio is terrible!
Problem #2: No Upper Bound
What's the maximum sentence length? We don't know! Some sentences have 10 words, others have 1000. The model would see wildly different scales during training, making it hard to learn stable patterns.
Problem #3: No Relative Position Information
The model can't easily learn "this word is 3 positions after that word." It would have to learn: "If this vector has +5 added and that vector has +2 added, they're 3 apart." That's hard to learn from data!
Attempt #2: Normalize Positions (Scale to 0.0 → 1.0)
"Okay, I see the problem! Let's scale positions to be between 0 and 1!"
The improved idea: Instead of using raw position numbers (1, 2, 3...), divide by the total sentence length to keep everything between 0 and 1.
For a 10-word sentence: "I really love eating delicious homemade pizza with my best friends"
Position 0: 0/10 = 0.0 → "I"
Position 1: 1/10 = 0.1 → "really"
Position 2: 2/10 = 0.2 → "love"
Position 3: 3/10 = 0.3 → "eating"
Position 4: 4/10 = 0.4 → "delicious"
Position 5: 5/10 = 0.5 → "homemade"
Position 6: 6/10 = 0.6 → "pizza"
Position 7: 7/10 = 0.7 → "with"
Position 8: 8/10 = 0.8 → "my"
Position 9: 9/10 = 0.9 → "best"
Position 10: 10/10 = 1.0 → "friends"
Great! All numbers are small (0.0 to 1.0), so they won't overwhelm our embeddings. Let's test it:
Adding to embeddings:
"I" (position 0.0):
Word embedding: [0.01, -0.20, 0.30, 0.15, -0.05, 0.11]
Position 0.0: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
──── ──── ──── ──── ──── ────
Combined: [0.01, -0.20, 0.30, 0.15, -0.05, 0.11] ✓ Unchanged (position 0)
"homemade" (position 0.5):
Word embedding: [-0.11, 0.88, 0.13, -0.55, 0.37, -0.71]
Position 0.5: [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
───── ──── ──── ───── ───── ─────
Combined: [ 0.39, 1.38, 0.63, -0.05, 0.87, -0.21] ✓ Balanced!
"friends" (position 1.0):
Word embedding: [0.90, -0.10, -0.20, -0.80, 0.50, 0.75]
Position 1.0: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
──── ──── ──── ──── ──── ────
Combined: [1.90, 0.90, 0.80, 0.20, 1.50, 1.75] ✓ Reasonable!
This looks much better! The position numbers (0.0 to 1.0) are the same scale as the word embeddings (−1 to +1), so neither overwhelms the other. The dimension values will encode BOTH word meaning AND position in a balanced way.
But now a new problem appears! Let's compare two different sentences:
Short sentence (5 words): "I love eating delicious pizza"
Position 0: 0/5 = 0.0
Position 1: 1/5 = 0.2
Position 2: 2/5 = 0.4
Position 3: 3/5 = 0.6
Position 4: 4/5 = 0.8
Distance between adjacent words: 0.2 - 0.0 = 0.2
Longer sentence (10 words): "I really love eating delicious homemade pizza with my friends"
Position 0: 0/10 = 0.0
Position 1: 1/10 = 0.1
Position 2: 2/10 = 0.2
...
Position 9: 9/10 = 0.9
Distance between adjacent words: 0.1 - 0.0 = 0.1
THE PROBLEM: Adjacent words are 0.2 apart in the 5-word sentence but only 0.1 apart in the 10-word sentence!
Why is this catastrophic?
Problem #1: Inconsistent "Next Word" Distance
The model is trying to learn: "When two words are next to each other, they probably relate to each other closely."
But "next to each other" means:
- In a 5-word sentence: 0.2 difference
- In a 10-word sentence: 0.1 difference
- In a 20-word sentence: 0.05 difference
- In a 100-word sentence: 0.01 difference
The model gets confused! It can't learn a consistent pattern for "these words are adjacent" because the numerical distance keeps changing depending on total sentence length.
Example with more detail:
Consider learning the pattern "adjective usually comes right before noun":
In 5-word sentence: "I love delicious homemade pizza"
"delicious" (position 0.6) → "pizza" (position 0.8)
Distance: 0.8 - 0.6 = 0.2
Model learns: "When distance ≈ 0.2, words are adjacent and adjective modifies noun"
In 20-word sentence: "Yesterday I was really very hungry so I love eating delicious homemade thin crust pizza from Italy with extra cheese"
"delicious" (position 0.45) → "homemade" (position 0.5)
Distance: 0.5 - 0.45 = 0.05
Model is confused: "Wait, 0.05 distance? That's tiny! But these words ARE adjacent..."
The model has to learn: "0.2 distance means adjacent in short sentences, but 0.05 means adjacent in long sentences." This is like saying "1 mile means 'nearby' when you're in a small town, but 'far away' when you're in a big city." Inconsistent!
Problem #2: Can't Extrapolate to Longer Sequences
During training, suppose we only see sentences up to 50 words long. The model learns position encodings from 0.0 to 1.0 based on these training examples.
During inference, we encounter a 100-word document. What happens?
The model still uses positions 0.0 to 1.0, but now:
- In training (50 words): position 0.5 meant "middle of sentence" = word 25
- In inference (100 words): position 0.5 means "middle" = word 50
The model learned: "Position 0.5 usually appears in sentence context X" (based on 50-word training) But now sees: "Position 0.5 in a very different context!" (100-word document)
The position encoding values get "reused" for different actual positions depending on sentence length, breaking the model's learned associations.
Problem #3: No Absolute Position Information
The model can't learn: "Words at the beginning of sentences tend to be capitalized" or "Question words often appear early."
Because "beginning" could be:
- Position 0.0-0.1 in a 10-word sentence
- Position 0.0-0.02 in a 50-word sentence
The "beginning" isn't a consistent numerical range—it depends on total length!
Summary of Attempt #2:
- ✓ Fixed: Bounded values (stay between 0 and 1)
- ✓ Fixed: Each position is unique
- ❌ Problem: Position spacing depends on sentence length
- ❌ Problem: "Adjacent words" have different numerical distances in different sentences
- ❌ Problem: Model can't learn consistent patterns
- ❌ Problem: Doesn't extrapolate well to longer sequences than training
We're getting closer, but we need something better!
Attempt #3: Different Scales Per Dimension
"What if we use DIFFERENT numbers for different dimensions? Instead of adding the same position to all dimensions, maybe each dimension can track position at a different scale!"
The smarter idea: Use different multipliers for each dimension, so they change at different rates.
For our 6 dimensions, let's try:
- Dimension 0: position × 1.0 (changes quickly)
- Dimension 1: position × 0.5 (changes medium speed)
- Dimension 2: position × 0.1 (changes slowly)
- Dimension 3: position × 0.05 (changes very slowly)
- Dimension 4: position × 0.01 (barely changes)
- Dimension 5: position × 0.001 (almost frozen)
Let's calculate for several positions:
Position 0:
Dimension 0: 0 × 1.0 = 0.0
Dimension 1: 0 × 0.5 = 0.0
Dimension 2: 0 × 0.1 = 0.0
Dimension 3: 0 × 0.05 = 0.0
Dimension 4: 0 × 0.01 = 0.0
Dimension 5: 0 × 0.001 = 0.0
Position encoding: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Position 1:
Dimension 0: 1 × 1.0 = 1.0 ⚡ (big change!)
Dimension 1: 1 × 0.5 = 0.5 🐇 (medium)
Dimension 2: 1 × 0.1 = 0.1 🐢 (small)
Dimension 3: 1 × 0.05 = 0.05 🐢
Dimension 4: 1 × 0.01 = 0.01 🐌 (tiny)
Dimension 5: 1 × 0.001 = 0.001 🐌
Position encoding: [1.0, 0.5, 0.1, 0.05, 0.01, 0.001]
Position 2:
Dimension 0: 2 × 1.0 = 2.0 ⚡ (changed a lot!)
Dimension 1: 2 × 0.5 = 1.0 🐇
Dimension 2: 2 × 0.1 = 0.2 🐢
Dimension 3: 2 × 0.05 = 0.1 🐢
Dimension 4: 2 × 0.01 = 0.02 🐌
Dimension 5: 2 × 0.001 = 0.002 🐌
Position encoding: [2.0, 1.0, 0.2, 0.1, 0.02, 0.002]
Position 5:
Dimension 0: 5 × 1.0 = 5.0 ⚡
Dimension 1: 5 × 0.5 = 2.5 🐇
Dimension 2: 5 × 0.1 = 0.5 🐢
Dimension 3: 5 × 0.05 = 0.25 🐢
Dimension 4: 5 × 0.01 = 0.05 🐌
Dimension 5: 5 × 0.001 = 0.005 🐌
Position encoding: [5.0, 2.5, 0.5, 0.25, 0.05, 0.005]
Position 10:
Dimension 0: 10 × 1.0 = 10.0 ⚡
Dimension 1: 10 × 0.5 = 5.0 🐇
Dimension 2: 10 × 0.1 = 1.0 🐢
Dimension 3: 10 × 0.05 = 0.5 🐢
Dimension 4: 10 × 0.01 = 0.1 🐌
Dimension 5: 10 × 0.001 = 0.01 🐌
Position encoding: [10.0, 5.0, 1.0, 0.5, 0.1, 0.01]
This is much better! Let's see what we achieved:
✓ Unique fingerprints: Each position has a distinct combination!
Position 1: [1.0, 0.5, 0.1, 0.05, 0.01, 0.001]
Position 2: [2.0, 1.0, 0.2, 0.1, 0.02, 0.002] ← Different from position 1!
Position 5: [5.0, 2.5, 0.5, 0.25, 0.05, 0.005] ← Different from both!
✓ Multi-scale information:
- Fast-changing dimensions (0, 1): Great for distinguishing nearby positions (position 1 vs 2)
- Slow-changing dimensions (4, 5): Great for distinguishing distant positions (position 10 vs 100)
The clock analogy becomes clear!
- Dimension 0 (× 1.0) = second hand (moves fast)
- Dimension 2 (× 0.1) = minute hand (moves medium)
- Dimension 4 (× 0.01) = hour hand (moves slow)
Together, they create unique "time signatures" for each position!
But we STILL have a problem! Watch what happens at longer positions:
Position 100:
Dimension 0: 100 × 1.0 = 100.0 ⚡ (HUGE!)
Dimension 1: 100 × 0.5 = 50.0 🐇 (BIG!)
Dimension 2: 100 × 0.1 = 10.0 🐢 (getting big...)
Dimension 3: 100 × 0.05 = 5.0 🐢
Dimension 4: 100 × 0.01 = 1.0 🐌 (reasonable)
Dimension 5: 100 × 0.001 = 0.1 🐌 (still small)
Position encoding: [100.0, 50.0, 10.0, 5.0, 1.0, 0.1]
Position 1000:
Dimension 0: 1000 × 1.0 = 1000.0 ⚡ (MASSIVE!)
Dimension 1: 1000 × 0.5 = 500.0 🐇 (ENORMOUS!)
Dimension 2: 1000 × 0.1 = 100.0 🐢 (VERY BIG!)
Dimension 3: 1000 × 0.05 = 50.0 🐢 (BIG!)
Dimension 4: 1000 × 0.01 = 10.0 🐌 (getting big...)
Dimension 5: 1000 × 0.001 = 1.0 🐌 (okay for now)
Position encoding: [1000.0, 500.0, 100.0, 50.0, 10.0, 1.0]
THE UNBOUNDED GROWTH PROBLEM RETURNS!
Even though we're using different scales, the values STILL grow without bound for long sequences!
Adding position 1000 to a word:
Word "document" at position 1000:
Word embedding: [0.45, -0.25, 0.60, -0.15, 0.30, 0.75] ← Word meaning (small)
Position encoding: [1000, 500, 100, 50, 10, 1 ] ← Position (HUGE!)
───── ───── ───── ───── ───── ─────
Combined: [1000.45, 499.75, 100.60, 49.85, 10.30, 1.75]
↑ ↑ ↑ ↑ ↑ ↑
Position dominates in first 5 dimensions!
The problem: Even dimension 4 (the "slow" dimension) reaches 10.0 at position 1000, which is 10× larger than our word embeddings (which are around ±1). The position signal is still shouting over the word meaning!
Why we're stuck:
Remember, each dimension after training needs to encode: word meaning + position
At position 1000, dimension 0 would try to encode:
- Word meaning: maybe 0.45 ("document-ness")
- Position marker: 1000
- Combined: 1000.45
The model sees "1000.45" and learns: "This is position 1000!" but struggles to notice the subtle 0.45 word meaning part. We're back to the same fundamental problem as Attempt #1!
Summary of Attempt #3:
- ✓ Fixed: Each position is unique (different combinations)
- ✓ Fixed: Multi-scale information (fast and slow dimensions)
- ✓ Improvement: Slower dimensions don't grow as quickly
- ❌ Problem: Values STILL grow unbounded for long sequences
- ❌ Problem: Eventually overwhelms word embeddings again (just takes longer to happen)
- ❌ Problem: No consistent upper bound
We're getting warmer! We have the RIGHT IDEA (different scales for different dimensions), but we need values that NEVER grow beyond a certain limit...
Part 3: The Breakthrough — Understanding Waves
What do we actually need? Let's step back and think clearly:
- Bounded values — Must stay in a reasonable range (like -1 to +1), never explode
- Unique patterns — Every position must have a different "fingerprint"
- Multi-scale information — Some dimensions change quickly, some slowly
- Consistent distances — Adjacent positions should always have similar spacing
- Works for any length — Must handle sequences longer than training
- Doesn't depend on sentence length — Position 5 should always mean "position 5", not "25% through the sentence"
Is this even possible?
Yes! And the answer comes from a beautiful area of mathematics: repeating patterns (also called periodic functions).
What's a Repeating Pattern?
You see repeating patterns everywhere in daily life:
Clock hands:
12 o'clock → 1 o'clock → 2 o'clock → ... → 11 o'clock → 12 o'clock → 1 o'clock → ...
The hour hand goes in circles, repeating every 12 hours
Days of the week:
Monday → Tuesday → Wednesday → ... → Sunday → Monday → Tuesday → ...
The pattern repeats every 7 days
Seasons:
Spring → Summer → Fall → Winter → Spring → Summer → Fall → Winter → ...
The pattern repeats every year
Waves in the ocean:
High → Low → High → Low → High → Low → ...
The water level goes up and down in a repeating cycle
Why Repeating Patterns are Perfect for Positions
Key insight: Repeating patterns give us bounded values (they never explode), but we can still create unique combinations!
Analogy: The Clock System
Imagine telling time using THREE clocks with different speeds:
Clock 1: Second hand (very fast ⚡)
- Completes a full circle every 60 seconds
- Position changes dramatically every second
- After 60 seconds: back to start, begins repeating
Clock 2: Minute hand (medium 🐇)
- Completes a full circle every 60 minutes
- Position changes noticeably every minute
- Takes much longer to repeat
Clock 3: Hour hand (slow 🐢)
- Completes a full circle every 12 hours
- Position barely moves minute-to-minute
- Takes forever to repeat
Now here's the magic: Even though each individual hand repeats, the COMBINATION of all three hands is unique for every moment!
Time: 10:30:05
Second hand: pointing at 1 (5 seconds)
Minute hand: pointing at 6 (30 minutes)
Hour hand: pointing between 10 and 11
This exact combination never happened before and won't happen again for 12 hours!
At 10:30:06 (just one second later):
Second hand: pointing at 1.2 (6 seconds) ← Changed!
Minute hand: pointing at 6 (30 minutes) ← Same
Hour hand: pointing between 10 and 11 ← Same
Different combination! Even though minute and hour hands didn't move much.
This is EXACTLY what we'll do with positions! We'll use multiple "clock hands" (repeating patterns) moving at different speeds. Each position gets a unique combination!
Introducing Sine and Cosine: Mathematical Waves
Sine and cosine are mathematical functions that create smooth, repeating wave patterns.
Don't panic if you don't know trigonometry! You don't need to understand the math deeply. Just understand what they DO:
What sine does:
- Takes a number as input (think: angle or position on a circle)
- Outputs a number between -1 and +1 (bounded! ✓)
- Creates a smooth wave that repeats forever
Visual description of the sine wave:
Start at 0 → rise up → reach peak (+1) → come back down →
cross 0 going down → reach valley (-1) → come back up → cross 0 going up →
repeat forever...
Understanding Sine and Cosine Numbers
Think of sine and cosine like this:
Imagine you're on a Ferris wheel:
- When you're at the bottom: height = -1
- When you're halfway up: height = 0
- When you're at the top: height = +1
- When you're halfway down: height = 0
- Back to bottom: height = -1
As the Ferris wheel rotates, your height smoothly goes up and down, always between -1 and +1. That's what sine does! It traces out your height as you go around the circle.
Sine (\sin) and Cosine (\cos) are functions that:
- Take an angle as input (think: position on a circle, measured in radians)
- Output a value between -1 and +1
- Create smooth, repeating wave patterns
📏 Quick note about radians: In machine learning, we ALWAYS use radians (not degrees) for trigonometric functions. Radians are a different way to measure angles:
- 0 radians = 0 degrees (starting point)
- π radians ≈ 3.14 radians = 180 degrees (halfway around circle)
- 2π radians ≈ 6.28 radians = 360 degrees (full circle)
When you see \sin(1), it means "sine of 1 radian" (about 57 degrees), not "sine of 1 degree"! Don't worry about converting—just know that the input numbers are radians, and one full wave cycle happens every 2\pi \approx 6.28 units.
Important clarification: We're NOT changing the sine function itself. The sine function is fixed—it's a mathematical function that always gives the same output for the same input. What we're changing is HOW QUICKLY we march through different input angles as position increases. This is what creates "fast" vs "slow" frequencies.
Sine Wave: Seeing the Full Pattern
Let's see actual numbers (you don't need to know how to calculate these—just observe the pattern). All values below are rounded to 2 decimal places, so when you see 1.00 or -1.00, read that as "very close to the peak or valley," not necessarily mathematically exact.
Input angle → Sine output
─────────────────────────
0.0 → 0.00 ← Starting point (middle of wave)
0.5 → 0.48 ← Rising...
1.0 → 0.84 ← Still rising...
1.5 → 1.00 ← Near the peak
2.0 → 0.91 ← Coming back down...
2.5 → 0.60 ← Still descending...
3.0 → 0.14 ← Almost at middle...
3.5 → -0.35 ← Crossed to negative!
4.0 → -0.76 ← Going deeper...
4.5 → -0.98 ← Almost at bottom...
5.0 → -0.96 ← Near the valley
5.5 → -0.71 ← Rising back up...
6.0 → -0.28 ← Still rising...
6.5 → 0.22 ← Crossed back to positive!
7.0 → 0.66 ← Climbing...
7.5 → 0.94 ← Near the top again!
8.0 → 0.99 ← Almost at peak!
8.5 → 0.76 ← Starting to descend...
9.0 → 0.41 ← Coming down...
9.5 → -0.08 ← Crossing to negative...
10.0 → -0.54 ← Negative again...
10.5 → -0.88 ← Going down...
11.0 → -1.00 ← Very near the bottom of the wave
11.5 → -0.88 ← Rising back up...
12.0 → -0.54 ← Still rising...
12.5 → -0.05 ← Almost back to zero...
See the pattern?
- Starts at 0
- Goes up to about +1 (peak)
- Comes back down through 0
- Goes down to about -1 (valley)
- Comes back up to 0
- Then repeats!
It takes about 6.28 units (called "2π" in math) to complete one full cycle, then the pattern repeats forever.
Key properties we love:
- ✓ Always bounded: Never goes above +1 or below -1
- ✓ Smooth changes: No sudden jumps
- ✓ Repeats forever: The pattern continues indefinitely
- ✓ Every point is different: Within one cycle, no two inputs give the exact same output
Cosine Wave: Sine's Twin
Cosine is almost identical to sine, just starts at a different point in the cycle. Again, the table below is rounded to 2 decimal places:
Input angle → Cosine output
────────────────────────────
0.0 → 1.00 ← Starting point (at the peak!)
0.5 → 0.88 ← Coming down...
1.0 → 0.54 ← Descending...
1.5 → 0.07 ← Almost at middle...
2.0 → -0.42 ← Crossed to negative!
2.5 → -0.80 ← Going deeper...
3.0 → -0.99 ← Near the valley
3.5 → -0.94 ← Rising back up...
4.0 → -0.65 ← Still rising...
4.5 → -0.21 ← Almost at middle...
5.0 → 0.28 ← Crossed back to positive!
5.5 → 0.71 ← Climbing...
6.0 → 0.96 ← Almost at peak...
6.5 → 0.99 ← Near top...
7.0 → 0.75 ← Starting to descend...
7.5 → 0.35 ← Coming down...
8.0 → -0.15 ← Crossing to negative...
8.5 → -0.61 ← Going down...
9.0 → -0.91 ← Near bottom...
9.5 → -0.99 ← Almost at valley...
10.0 → -0.84 ← Rising back up...
10.5 → -0.53 ← Still rising...
11.0 → -0.00 ← Very close to the middle again
11.5 → 0.53 ← Climbing...
12.0 → 0.84 ← Rising...
12.5 → 0.99 ← Almost at peak again...
Think of it this way:
- Sine: Starts at middle (0), goes up first
- Cosine: Starts at peak (+1), goes down first
- Same wave shape, different starting position!
Like two runners on a circular track—one starts at the starting line, the other starts a quarter-lap ahead. They're running the same loop, just offset!
Why Use BOTH Sine and Cosine?
Great question! We could theoretically use just sine, but using BOTH gives us more information:
Analogy: Describing a location on a circle
Imagine you're standing somewhere on a circular track. To tell someone EXACTLY where you are, you need TWO pieces of information:
Just distance traveled: "I'm 5 meters along the track"
Problem: Which direction? Going clockwise or counterclockwise? Am I on lap 1 or lap 2?
Better: "I'm at coordinates (3m East, 4m North from center)"
Perfect! This tells me exactly where you are! No ambiguity.
Sine and cosine are like "horizontal position" and "vertical position" on a circle. Together, they give complete information about where you are in the wave cycle!
For each dimension (position tracking), using both gives us:
- Sine value: How far up/down in the wave?
- Cosine value: Rising or falling? Which phase of the cycle?
- Together: Exact location in the cycle, no ambiguity!
This becomes especially important when we're combining multiple frequencies—the sine/cosine pairs help the model distinguish positions more precisely.
Wait—What Does "Speed" or "Frequency" Actually Mean Here?
Great question! Let's clarify this because it's often confusing:
Remember: Sine takes an angle as input and returns a value (between -1 and +1).
sin(angle) = value
So when we talk about "speed" or "frequency," we're NOT talking about the sine function itself changing—we're talking about how quickly we advance through the angles as position increases!
Slow Frequency (Slow Speed 🐢):
Position 0 → angle = 0.0 → sin(0.0) = 0.00
Position 1 → angle = 0.002 → sin(0.002) = 0.002 (barely moved!)
Position 2 → angle = 0.004 → sin(0.004) = 0.004 (still barely changed)
Position 3 → angle = 0.006 → sin(0.006) = 0.006
...
Position 100 → angle = 0.2 → sin(0.2) = 0.199 (finally moving noticeably)
The angles advance SLOWLY as position increases, so the sine values change SLOWLY. It takes many positions before you see significant changes!
Fast Frequency (Fast Speed ⚡):
Position 0 → angle = 0 → sin(0) = 0.00
Position 1 → angle = 1 → sin(1) = 0.84 (big jump!)
Position 2 → angle = 2 → sin(2) = 0.91 (changed a lot!)
Position 3 → angle = 3 → sin(3) = 0.14 (different again!)
Position 4 → angle = 4 → sin(4) = -0.76 (completely different!)
The angles advance QUICKLY as position increases, so the sine values change QUICKLY. Every position looks very different!
The Formula Controls the Speed:
In the formula \sin\left(\frac{\text{pos}}{10000^{2i/d_{\text{model}}}}\right), the denominator (10000^{2i/d_{\text{model}}}) controls how fast we advance through angles:
-
Large denominator (like 464.159) → angle advances SLOWLY → slow frequency 🐢
Position 1: 1/464.159 = 0.002(tiny angle)Position 2: 2/464.159 = 0.004(still tiny)
-
Small denominator (like 1.0) → angle advances QUICKLY → fast frequency ⚡
Position 1: 1/1 = 1(big angle)Position 2: 2/1 = 2(bigger angle)
So "speed" or "frequency" refers to: How fast do we march through the sine wave as position increases?
- Fast frequency = take big steps through angles = values change dramatically position-to-position
- Slow frequency = take tiny steps through angles = values barely change position-to-position
Why use different speeds? Because combining fast-changing dimensions with slow-changing dimensions creates unique fingerprints! If all dimensions changed at the same speed, different positions might look too similar.
To summarize Part 3:
- ✓ Sine and cosine create bounded values (always between -1 and +1)
- ✓ They create repeating patterns (like clock hands going in circles)
- ✓ Using multiple speeds (frequencies) together creates unique combinations for each position
- ✓ Like a clock with second, minute, and hour hands—each position in time has a unique combination of hand positions!
Now let's see exactly HOW we turn this brilliant idea into actual numbers...
Part 4: Building the Solution Step-by-Step
Now we're ready to construct the actual positional encoding formula. We'll build it piece by piece, explaining every single component.
The Complete Formula (Overview First)
For position \text{pos} and dimension pair i:
Don't panic! Let's break this down into bite-sized pieces. By the end, you'll understand every symbol.
Step 1: Understanding Dimension Pairs
Key concept: Dimensions come in pairs. Each pair uses the same frequency (speed), but one uses sine and one uses cosine.
For d_{\text{model}} = 6 (our example):
Pair 0 (i = 0):
- Dimension 0: uses sine
- Dimension 1: uses cosine
- Both at the same frequency (very fast)
Pair 1 (i = 1):
- Dimension 2: uses sine
- Dimension 3: uses cosine
- Both at the same frequency (medium)
Pair 2 (i = 2):
- Dimension 4: uses sine
- Dimension 5: uses cosine
- Both at the same frequency (slow)
Why pairs? As we explained earlier, having both sine and cosine at each frequency gives us complete information about where we are in the wave cycle—no ambiguity!
Step 2: The Magic Number 10,000
The original Transformer paper chose 10,000 as a base number. Why?
The reasoning:
- We want the slowest wave to change VERY slowly
- So it can distinguish positions even in very long sequences (thousands of words)
- 10,000 is large enough that even at position 1000, the slowest wave has barely completed one cycle
Think of it like this:
- If we used 100: The slow wave completes a cycle every ~628 positions (100 × 2π ≈ 628)
- Using 10,000: The slow wave completes a cycle every ~62,800 positions!
This means even in a document with 10,000 words, our slowest wave is still providing unique position information! 🎯
The choice of 10,000 is somewhat arbitrary—researchers found it works well. You could use 5,000 or 20,000, but 10,000 has become the standard.
Step 3: Computing the Frequency for Each Dimension Pair
Now, we need to figure out: "For dimension pair i, what divisor (frequency) should we use?"
We want:
- Pair 0 (i=0): divisor ≈ 1 (fastest)
- Pair 1 (i=1): divisor ≈ medium
- Pair 2 (i=2): divisor ≈ 10,000 (slowest)
And they should be evenly spread on a logarithmic scale (not bunched up).
The formula for this: Use exponential scaling!
Let's calculate this for our example (d_{\text{model}} = 6):
For pair 0 (i = 0, dimensions 0 & 1):
Anything to the power of 0 equals 1! So we divide by 1 (fastest wave).
For pair 1 (i = 1, dimensions 2 & 3):
What's 10000^{1/3}? It's the cube root of 10,000:
So we divide by ~21.5 (medium wave).
For pair 2 (i = 2, dimensions 4 & 5):
What's 10000^{2/3}? It's the cube root of 10,000, squared:
So we divide by ~464 (slow wave).
Let's see the pattern:
| Pair | i | Exponent 2i/d_{\text{model}} | Divisor 10000^{\text{exponent}} | Speed |
|---|---|---|---|---|
| 0 | 0 | 0/6 = 0.000 | 10000^{0.000} = 1 | ⚡⚡⚡ Very Fast |
| 1 | 1 | 2/6 = 0.333 | 10000^{0.333} ≈ 21.5 | 🐇 Medium |
| 2 | 2 | 4/6 = 0.667 | 10000^{0.667} ≈ 464 | 🐢 Slow |
Beautiful! The exponents (0, 0.333, 0.667) are evenly spaced between 0 and 1.
And because we're raising 10,000 to these powers, the divisors are evenly spread on a logarithmic scale:
- 1 → 21.5 is a jump of ~21×
- 21.5 → 464 is a jump of ~21×
Each pair is about 21× slower than the previous! This logarithmic spacing helps us cover a HUGE range (from 1 to 10,000) smoothly.
Why logarithmic spacing? If we used linear spacing (like 1, 5000, 10000), all the "interesting" changes would be bunched up at the beginning, and we'd waste dimensions. Logarithmic spacing gives us useful information at all scales!
Step 4: Putting It All Together
Now let's understand the complete formula piece by piece:
Let's decode every piece:
\text{PE}(\text{pos}, \text{dim})
- "Positional Encoding for position
pos, dimensiondim" - This is the number we're calculating
\text{pos}
- The position in the sentence (0, 1, 2, 3, ...)
- Position 0 = first word, position 1 = second word, etc.
2i (for even dimensions)
- i is the "pair index" (0, 1, 2, ...)
- When i=0: dimension 2i=0 (even, uses sine)
- When i=1: dimension 2i=2 (even, uses sine)
- When i=2: dimension 2i=4 (even, uses sine)
2i+1 (for odd dimensions)
- When i=0: dimension 2i+1=1 (odd, uses cosine)
- When i=1: dimension 2i+1=3 (odd, uses cosine)
- When i=2: dimension 2i+1=5 (odd, uses cosine)
d_{\text{model}}
- The total number of dimensions (6 in our case)
- Remember, this is the size of each word vector
10000^{2i/d_{\text{model}}}
- The divisor that controls wave speed
- For early pairs (i=0): small divisor (1) → fast wave
- For middle pairs (i=1): medium divisor (~21.5) → medium wave
- For late pairs (i=2): large divisor (~464) → slow wave
\sin(\ldots) and \cos(\ldots)
- The wave functions that create the repeating patterns
- Sine for even dimensions, cosine for odd dimensions
- Both bounded between -1 and +1
\frac{\text{pos}}{10000^{2i/d_{\text{model}}}}
- This is the angle we feed into sine/cosine
- As position increases, this angle increases
- How fast it increases depends on the divisor
The complete logic:
- Pick a position (e.g., position 2 = third word)
- Pick a dimension (e.g., dimension 0)
- Figure out which pair this dimension belongs to (i = 0 for dim 0)
- Calculate the divisor: 10000^{0/6} = 1
- Calculate the angle: \text{pos}/\text{divisor} = 2/1 = 2
- Apply sine (since dimension 0 is even): \sin(2) ≈ 0.909
- That's the positional encoding value for position 2, dimension 0!
Dimension Speed Summary:
| Dimension | Pair i | Formula | Divisor | Speed | Purpose |
|---|---|---|---|---|---|
| 0 (sine) | 0 | 10000^{0/6} | 1.0 | ⚡⚡⚡ | Distinguish adjacent positions |
| 1 (cos) | 0 | 10000^{0/6} | 1.0 | ⚡⚡⚡ | (same, but phase-shifted) |
| 2 (sine) | 1 | 10000^{2/6} | 21.5 | 🐇 | Distinguish nearby groups |
| 3 (cos) | 1 | 10000^{2/6} | 21.5 | 🐇 | (same, but phase-shifted) |
| 4 (sine) | 2 | 10000^{4/6} | 464.2 | 🐢 | Distinguish distant regions |
| 5 (cos) | 2 | 10000^{4/6} | 464.2 | 🐢 | (same, but phase-shifted) |
Key insight: Each word's position is encoded using ALL 6 dimensions together. The fast dimensions tell us the exact local position, medium dimensions tell us the local neighborhood, and slow dimensions tell us the broad region. Together, they create a unique 6-number "fingerprint" for every single position!
Part 5: Calculating Position Encodings (Hands-On!)
Now let's actually COMPUTE the position encodings! We'll calculate every dimension for multiple positions so you can see the patterns emerge.
Setting Up Our Calculation
We have:
- 3 positions for math (0, 1, 2) for "I love pizza"
- But we'll calculate 10 positions (0-9) to see the patterns!
- 6 dimensions (0, 1, 2, 3, 4, 5)
- Need to calculate position encodings for each!
First: Calculate All the Divisors
Before we start, let's compute the divisors for each dimension pair once:
For dimensions 0 & 1 (pair i=0):
For dimensions 2 & 3 (pair i=1):
For dimensions 4 & 5 (pair i=2):
Divisor Reference Table:
| Dimension | Pair i | Divisor | Speed |
|---|---|---|---|
| 0 (sine) | 0 | 1.000 | ⚡⚡⚡ |
| 1 (cos) | 0 | 1.000 | ⚡⚡⚡ |
| 2 (sine) | 1 | 21.544 | 🐇 |
| 3 (cos) | 1 | 21.544 | 🐇 |
| 4 (sine) | 2 | 464.159 | 🐢 |
| 5 (cos) | 2 | 464.159 | 🐢 |
Great! Now we can calculate each position.
Calculating Position Encodings for "I love pizza"
Let's calculate the encoding for position 0, 1, and 2 (our three words) with complete detail!
Position 0 ("I"):
For dim₀ (even, i=0):
For dim₁ (odd, i=0):
For dim₂ (even, i=1):
For dim₃ (odd, i=1):
For dim₄ (even, i=2):
For dim₅ (odd, i=2):
Position 0 encoding: [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
Position 1 ("love"):
Let me calculate the denominators first:
- Dims 0,1: 10000^{0/6} = 10000^0 = 1
- Dims 2,3: 10000^{2/6} = 10000^{1/3} = 21.544
- Dims 4,5: 10000^{4/6} = 10000^{2/3} = 464.159
Now the values:
Position 1 encoding: [0.84, 0.54, 0.05, 1.00, 0.00, 1.00]
Position 2 ("pizza"):
Position 2 encoding: [0.91, -0.42, 0.09, 1.00, 0.00, 1.00]
Summary of what we calculated:
Position 0: [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
Position 1: [0.84, 0.54, 0.05, 1.00, 0.00, 1.00]
Position 2: [0.91, -0.42, 0.09, 1.00, 0.00, 1.00]
Notice how each position gets a UNIQUE 6-number fingerprint! Position 0 is different from position 1, which is different from position 2. The fast-changing dimensions (0, 1) vary a lot, while slow-changing dimensions (4, 5) barely change for these early positions.
Seeing the Pattern: Let's Look at More Positions!
Three positions aren't enough to really SEE the beautiful pattern. Let's calculate positions 0 through 9 (imagine a 10-word sentence) so you can watch the waves dance!
Position Encoding Table (rounded for clarity):
| Position | dim₀ | dim₁ | dim₂ | dim₃ | dim₄ | dim₅ |
|---|---|---|---|---|---|---|
| 0 | 0.00 | 1.00 | 0.00 | 1.00 | 0.00 | 1.00 |
| 1 | 0.84 | 0.54 | 0.05 | 1.00 | 0.00 | 1.00 |
| 2 | 0.91 | -0.42 | 0.09 | 1.00 | 0.00 | 1.00 |
| 3 | 0.14 | -0.99 | 0.14 | 0.99 | 0.01 | 1.00 |
| 4 | -0.76 | -0.65 | 0.19 | 0.98 | 0.01 | 1.00 |
| 5 | -0.96 | 0.28 | 0.23 | 0.97 | 0.01 | 1.00 |
| 6 | -0.28 | 0.96 | 0.28 | 0.96 | 0.01 | 1.00 |
| 7 | 0.66 | 0.75 | 0.32 | 0.95 | 0.02 | 1.00 |
| 8 | 0.99 | -0.15 | 0.37 | 0.93 | 0.02 | 1.00 |
| 9 | 0.41 | -0.91 | 0.42 | 0.91 | 0.02 | 1.00 |
Now look at the patterns!
Dimensions 0 & 1 (Very Fast ⚡⚡⚡):
- Change dramatically with every position!
- Position 0: [0.00, 1.00]
- Position 1: [0.84, 0.54] — totally different!
- Position 2: [0.91, -0.42] — different again!
- They wiggle up and down like a jump rope: positive, negative, positive, negative...
Dimensions 2 & 3 (Medium 🐇):
- Change gradually
- Position 0: [0.00, 1.00]
- Position 5: [0.23, 0.97] — small shift
- Position 9: [0.42, 0.91] — noticeable but gentle
Dimensions 4 & 5 (Slow 🐢):
- Barely move at all for these early positions!
- Position 0: [0.00, 1.00]
- Position 9: [0.02, 1.00] — almost identical!
- These would become more useful for distinguishing position 100 vs position 200
The magic: Each row is COMPLETELY UNIQUE! No two positions share the same 6-number pattern. It's like each position has its own special fingerprint, QR code, or secret handshake!
Try picking any two rows — they're always different. That's how the model knows position 3 is not position 7, even though both are just "somewhere in the middle."
Part 6: Combining Position with Word Meaning
Now comes the crucial part: we need to COMBINE the position information with the word meaning.
Recall from Chapter 3, our word embeddings are:
"I": [0.01, -0.20, 0.30, 0.15, -0.05, 0.11]
"love": [-0.40, 0.60, 0.00, 0.25, 0.90, -0.30]
"pizza": [0.90, -0.10, -0.20, -0.80, 0.50, 0.75]
These capture the MEANING of each word.
And from Part 5, we now have position encodings:
Position 0: [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
Position 1: [0.84, 0.54, 0.05, 1.00, 0.00, 1.00]
Position 2: [0.91, -0.42, 0.09, 1.00, 0.00, 1.00]
These capture the POSITION of each word.
How do we combine them? Simple element-wise addition!
What is "element-wise" addition? We add corresponding numbers:
- First number + first number
- Second number + second number
- Third number + third number
- And so on...
Example with small numbers:
[1, 2, 3] + [10, 20, 30] = [11, 22, 33]
Each position gets added independently. It's like having two bank accounts and combining their balances!
Adding Position to "I" (Position 0)
Let's go dimension by dimension:
Word embedding "I": [0.01, -0.20, 0.30, 0.15, -0.05, 0.11]
Position 0 encoding: [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
──── ──── ──── ──── ──── ────
Dimension 0: 0.01 + 0.00 = 0.01 (word meaning barely changes)
Dimension 1: -0.20 + 1.00 = 0.80 (position adds a lot here!)
Dimension 2: 0.30 + 0.00 = 0.30 (word meaning unchanged)
Dimension 3: 0.15 + 1.00 = 1.15 (position adds a lot here!)
Dimension 4: -0.05 + 0.00 = -0.05 (word meaning unchanged)
Dimension 5: 0.11 + 1.00 = 1.11 (position adds a lot here!)
──── ──── ──── ──── ──── ────
Combined result: [0.01, 0.80, 0.30, 1.15, -0.05, 1.11]
Key insight: The result contains BOTH signals!
- Dimensions 0, 2, 4: Mostly the original word embedding (position added ~0)
- Dimensions 1, 3, 5: Mix of word embedding and position (position added 1.0)
The word "I" now carries both its meaning AND its position!
Adding Position to "love" (Position 1)
Word embedding "love": [-0.40, 0.60, 0.00, 0.25, 0.90, -0.30]
Position 1 encoding: [ 0.84, 0.54, 0.05, 1.00, 0.00, 1.00]
───── ───── ───── ───── ───── ─────
Dimension 0: -0.40 + 0.84 = 0.44 (big shift from position!)
Dimension 1: 0.60 + 0.54 = 1.14 (both contribute)
Dimension 2: 0.00 + 0.05 = 0.05 (small position signal)
Dimension 3: 0.25 + 1.00 = 1.25 (position dominates)
Dimension 4: 0.90 + 0.00 = 0.90 (word meaning preserved)
Dimension 5: -0.30 + 1.00 = 0.70 (position shifts it)
───── ───── ───── ───── ───── ─────
Combined result: [ 0.44, 1.14, 0.05, 1.25, 0.90, 0.70]
Notice how position 1 has different values than position 0! The word "love" now encodes:
- What it means (from the embedding)
- Where it is (from the position encoding)
Adding Position to "pizza" (Position 2)
Word embedding "pizza": [ 0.90, -0.10, -0.20, -0.80, 0.50, 0.75]
Position 2 encoding: [ 0.91, -0.42, 0.09, 1.00, 0.00, 1.00]
───── ───── ───── ───── ───── ─────
Dimension 0: 0.90 + 0.91 = 1.81 (both contribute large values!)
Dimension 1: -0.10 + -0.42 = -0.52 (both negative, add together)
Dimension 2: -0.20 + 0.09 = -0.11 (partially cancel out)
Dimension 3: -0.80 + 1.00 = 0.20 (position wins over word)
Dimension 4: 0.50 + 0.00 = 0.50 (word meaning preserved)
Dimension 5: 0.75 + 1.00 = 1.75 (both contribute)
───── ───── ───── ───── ───── ─────
Combined result: [ 1.81, -0.52, -0.11, 0.20, 0.50, 1.75]
Observation: Position 2 has yet another unique pattern! Different from positions 0 and 1.
The Final Position-Aware Matrix
Stacking all three combined vectors:
What do we have now?
- Row 1 = "I" at position 0 (word meaning + position info)
- Row 2 = "love" at position 1 (word meaning + position info)
- Row 3 = "pizza" at position 2 (word meaning + position info)
Each vector is now position-aware! The transformer can tell:
- What each word means (from the original embedding component)
- Where each word is (from the position encoding component)
- That "I" comes first, "love" comes second, "pizza" comes third
Analogy: Think of it like a name tag at a conference:
- Your NAME (word embedding) tells people who you are
- Your SEAT NUMBER (position encoding) tells people where you're sitting
- Both pieces of information are printed on the same badge (combined vector)
The sine waves create endless unique patterns—position 0 is different from position 1, which is different from position 2, and so on forever!
Part 7: Critical Question About Training
Won't Training Mess Up These Positions?
"Wait! Now I see we ADDED position numbers to the embeddings. But during training, won't the model UPDATE these numbers? Won't the position information get changed or lost?"
Excellent question! This is the #1 confusion point. Let me clarify:
SHORT ANSWER: The positional encodings are COMPLETELY FROZEN. They NEVER get updated during training. Only the word embeddings get updated.
Let me show exactly what happens:
BEFORE training (what we just calculated above):
Word embedding "pizza": [0.90, -0.10, -0.20, -0.80, 0.50, 0.75] ← RANDOM INITIAL VALUES
Position 2 encoding: [0.91, -0.42, 0.09, 1.00, 0.00, 1.00] ← FROM FIXED FORMULA
───── ───── ───── ───── ───── ─────
Combined: [1.81, -0.52, -0.11, 0.20, 0.50, 1.75] ← WHAT TRANSFORMER SEES
AFTER some training (embeddings learn better values):
Word embedding "pizza": [0.88, -0.15, -0.18, -0.79, 0.48, 0.73] ← CHANGED! (learned)
Position 2 encoding: [0.91, -0.42, 0.09, 1.00, 0.00, 1.00] ← STILL EXACT SAME!
───── ───── ───── ───── ───── ─────
Combined: [1.79, -0.57, -0.09, 0.21, 0.48, 1.73] ← NEW COMBINED VECTOR
See what happened?
- The word embedding LEARNED better values (from [0.90, -0.10...] to [0.88, -0.15...])
- The position encoding STAYED EXACTLY THE SAME ([0.91, -0.42...] never changed!)
- The combined result changed, but only because the embedding changed
How is this possible?
During training:
- We compute the combined vector (embedding + position)
- This flows through the transformer
- We compute gradients (how to improve)
- BUT: We only apply updates to the embedding matrix
- The position encodings are marked as "frozen" (no gradients computed)
The Graph Paper Analogy (This makes it crystal clear!):
Imagine you're drawing on graph paper:
- The grid lines (position encodings) are printed on the paper — they're fixed, permanent, part of the paper itself
- The dots you draw (word embeddings) are what you're learning to place correctly
- As you practice drawing, you move the dots around to find the best positions ON THE FIXED GRID
- The grid lines never move — they're the stable reference frame
- You learn: "A dot at position (2, 3) on the grid means something different than a dot at position (5, 7)"
Same here! The model LEARNS to place word meanings correctly on the fixed position grid. The embeddings move and adjust, but the grid (positions) stays constant.
Another way to think about it: Position encodings are like the ruler on your desk. You don't bend or change the ruler when you're learning to measure things — it stays fixed, and you learn to read and use it correctly!
The math (for those curious):
When we compute gradients via backpropagation:
The gradient with respect to the embedding:
The gradient just flows straight through! It's like: (x + 5) — if we update x, the 5 stays constant.
In practice: The code literally marks position encodings as "requires_grad=False" so the optimizer ignores them during updates.
Why this works beautifully:
- Positions are COMPUTED from a formula (not learned parameters)
- They provide a stable reference frame
- Word embeddings learn to encode meaning WITHIN that fixed frame
- The model learns: "dimension 1 having value ~0.5 at position 1 means X, but the same value at position 2 means Y"
Part 8: Why This Solution Is Beautiful
Let's recap why sine/cosine positional encodings solve all our requirements:
✓ Bounded: Values stay between -1 and +1, never drowning out the word embeddings ✓ Unique: Every position gets a unique 6-number fingerprint ✓ Multi-scale: Fast-changing dimensions (high frequency) + slow-changing dimensions (low frequency) = unique combinations ✓ Relative positions: The model can learn "these words are 3 apart" because sine waves have mathematical relationships ✓ Infinite length: Works for any sequence length, even longer than training (sine waves never run out!) ✓ No training needed: Computed from a formula, no extra parameters to learn ✓ Frozen during training: Position information stays constant; only word embeddings learn
Part 9: Summary and Key Takeaways
Before moving on, make sure you understand:
- The problem: Transformers process all words in parallel, so they can't tell word order without help
- Simple solutions fail: Using 1, 2, 3 has scale problems; normalizing has sentence-length dependencies
- Sine/cosine solution: Creates bounded, unique, multi-scale position fingerprints
- Frozen positions: Position encodings NEVER change during training—only word embeddings learn
- Addition, not replacement: We ADD position info to embeddings; both signals coexist
- The model learns to interpret: Embeddings adjust to work with the fixed position signals
Most important insight: The position encodings are like a fixed coordinate system. The word embeddings learn to position themselves meaningfully within that fixed system. It's not that positions "get in the way"—they provide a stable reference frame!
Modern Alternatives (For Completeness)
While we use sinusoidal encodings in this tutorial, modern transformers experiment with alternatives:
- Learned positional embeddings: Learn positions like word embeddings (simpler but can't extrapolate beyond training length)
- RoPE (Rotary Position Embedding): Encodes position by rotating embeddings instead of adding a separate position vector. This makes relative distances between words easier for the model to track (used in LLaMA, GPT-NeoX)
- ALiBi (Attention with Linear Biases): Adds position info directly to attention scores (simpler, very effective)
Each has trade-offs, but sinusoidal encoding remains elegant and effective for understanding the core concepts!
🎓 Advanced Note: The Mathematical Elegance (For the Curious)
This section is OPTIONAL! If you're satisfied with understanding that sinusoidal encodings create unique, bounded position fingerprints, you can skip this. But if you're curious about the deeper mathematical beauty, read on!
The question: We explained that sine/cosine encodings have many nice properties (bounded, unique, multi-scale). But there's ONE more profound property that makes them mathematically elegant: relative position can be computed as a linear transformation.
What does this mean?
For any fixed offset k (like "3 positions away"), there exists a matrix M_k such that:
In plain English: The encoding for "position + 3" can be computed from the encoding for "position" using a simple matrix multiplication, regardless of what "position" is!
Why this is profound:
Without this property:
- Model needs to learn: "What does +3 positions mean at position 5? What about at position 50? What about at position 500?"
- Different rule for every starting position!
- Requires learning infinite patterns
With this property:
- Model learns ONE matrix M_3 that means "+3 positions"
- Works at position 5, 50, 500—everywhere!
- Single learned transformation handles all cases
The mathematical reason (trigonometric identities):
These can be written as matrix operations! For offset k:
The matrix
is the rotation matrix for offset k!
The practical implication: The attention mechanism (which uses matrix multiplications) can easily learn: "When comparing word A at position p with word B at position p+3, I should look for pattern X." The "+3" relationship is expressible as a matrix operation the model can learn!
This is why sine/cosine isn't just a "clever trick"—it's a principled mathematical solution! Other functions (like polynomials or exponentials) don't have this property. Sine and cosine are special because of these trigonometric identities.
This is the insight that makes sinusoidal encodings not just "working in practice" but "elegant in theory." It's why they were chosen in the original "Attention Is All You Need" paper, beyond just being bounded and unique.
Don't worry if this seems advanced! You don't NEED to understand this to use transformers effectively. It's mathematical icing on the cake—interesting for those who want to go deeper!
Course navigation: Previous: Chapter 3 - Embeddings | Next: Chapter 5 - Multi-Head Self-Attention