Before we dive in, think about these critical questions:
- Why does self-attention ignore word order?
- How are "Ali killed lion" and "Lion killed Ali" the same to a Transformer?
- Why can't we just add position numbers (1, 2, 3, ...) to embeddings?
- What's wrong with discrete position values?
- How can sine and cosine functions encode position?
- Why do we ADD positional encoding instead of CONCATENATING it?
Let's solve the biggest problem in self-attention!
Our beautiful self-attention mechanism has a fatal flaw!
Sentence 1: "Ali killed the lion"
Sentence 2: "Lion killed Ali"
Self-Attention sees them as: IDENTICAL! ❌
Self-attention processes all words in PARALLEL.
# For both sentences:
words = ["Ali", "killed", "the", "lion"] # Same words!
# Attention computation (simplified)
for word in words:
attention_score = Query · Key # No position info!
# Order doesn't matter in this computation!The attention mechanism is a SET operation, not a SEQUENCE operation!
Sentence 1: "Ali killed the lion"
Self-Attention Output (simplified):
Ali → {0.1×killed, 0.7×lion, 0.2×the}
killed → {0.5×Ali, 0.3×lion, 0.2×the}
lion → {0.6×Ali, 0.3×killed, 0.1×the}
Sentence 2: "Lion killed Ali"
Self-Attention Output (simplified):
Lion → {0.6×Ali, 0.3×killed, 0.1×the}
killed → {0.5×Ali, 0.3×lion, 0.2×the}
Ali → {0.1×killed, 0.7×lion, 0.2×the}
Same attention patterns, just reordered! The MEANING is completely different, but the model can't tell!
- ✅ Bounded values (not unbounded like 1, 2, 3, ...)
- ✅ Continuous (not discrete)
- ✅ Unique for each position
- ✅ Captures relative positioning (distance between words)
- ✅ Works with neural networks (gradient-friendly)
- ✅ Generalizes to unseen sequence lengths
Let's explore different approaches...
# Just add position numbers!
position = [1, 2, 3, 4, ...]
# For "Ali killed the lion"
Ali: embedding + 1
killed: embedding + 2
the: embedding + 3
lion: embedding + 4Sentence 1 (4 words): [1, 2, 3, 4]
Sentence 2 (10 words): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
PDF document (100,000 words): [1, 2, 3, ..., 100000]
What's the upper limit? 1 lac? 2 lac? ∞?
Problem: Neural networks struggle with very large numbers!
- Gradient instability
- Vanishing/exploding gradients
- Poor generalization
Original embedding: 512 dimensions
Add position: +1 dimension
New total: 513 dimensions
This changes the entire architecture! ❌
We want to keep the same dimension (512).
# Normalize position to [0, 1] range
position = word_index / total_words
# For 4-word sentence:
Ali: 1/4 = 0.25
killed: 2/4 = 0.50
the: 3/4 = 0.75
lion: 4/4 = 1.00Sentence 1 (4 words):
Position 2 → 2/4 = 0.50
Sentence 2 (10 words):
Position 2 → 2/10 = 0.20
Position 2 has DIFFERENT encodings in different sentences! ❌
The model can't learn consistent positional patterns.
Both approaches use discrete integers.
Position: 1, 2, 3, 4, 5, ...
Gaps between values:
Between 1 and 2: nothing
Between 2 and 3: nothing
Neural networks prefer continuous values for smooth gradients!
# Can you tell the distance between positions?
pos_1 = 1
pos_5 = 5
# Distance = 5 - 1 = 4 ✓
# But for the model:
# These are just independent numbers!
# No built-in way to compute relative distance ❌Use periodic functions (sine and cosine) with different frequencies!
PE(pos, i) = {
sin(pos / 10000^(2i/d_model)) if i is even
cos(pos / 10000^(2i/d_model)) if i is odd
}
where:
pos = position in sequence (0, 1, 2, 3, ...)
i = dimension index (0, 1, 2, ..., d_model-1)
d_model = embedding dimension (e.g., 512)sin(x), cos(x/2), sin(x/3), cos(x/4)
1.00 ┤ ╭╮ ╭╮ ╭╮
│ ╱ ╲ ╱ ╲ ╱ ╲
0.75 ┤ ╱ ╲ ╱ ╲ ╱ ╲
│ ╱ ╲ ╱ ╲ ╱ ╲
0.50 ┤ ╱ ╳ ╳ ╲
│╱ ╱ ╲ ╱ ╲ ╲
0.25 ┤ ╱ ╲ ╱ ╲ ╱ ╲
│ ╱ ╲ ╱ ╲ ╱ ╲
0.00 ┼─────────────────────────────────
│ ╲ ╱ ╲ ╱ ╲ ╱
-0.25 ┤ ╲ ╱ ╲ ╱ ╲ ╱
│ ╲ ╱ ╲ ╱ ╲ ╱
-0.50 ┤ ╳ ╳ ╳
│ ╱ ╲ ╱ ╲ ╱ ╲
-0.75 ┤ ╱ ╲ ╱ ╲ ╱ ╲
│ ╱ ╲ ╱ ╲ ╱ ╲
-1.00 ┤ ╯ ╰╯ ╰╯ ╰
└────────────────────────────────
0 5 10 15 20 25 30
Position
Different frequencies create unique patterns!
sin(x) and cos(x) always in range [-1, 1]
No matter the position:
PE(1, i) ∈ [-1, 1]
PE(100, i) ∈ [-1, 1]
PE(100000, i) ∈ [-1, 1]
Perfect for neural networks! ✓
sin(x) and cos(x) are smooth, differentiable functions
Gradient-friendly! ✓
Each position gets a unique combination of sine/cosine values
across all dimensions
Position 1: [sin(1/10000^0), cos(1/10000^0.004), ...]
Position 2: [sin(2/10000^0), cos(2/10000^0.004), ...]
Position 3: [sin(3/10000^0), cos(3/10000^0.004), ...]
All different! ✓
The key property:
PE(pos + k) can be represented as a LINEAR FUNCTION of PE(pos)
Mathematically:
PE(pos + k) = T × PE(pos)
where T is a transformation matrix
This means the model can LEARN relative distances through matrix multiplication!
We'll explore this in detail later.
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
where:
pos = position (0, 1, 2, 3, ...)
i = dimension index (0, 1, 2, ..., d_model/2 - 1)
2i = even dimensions (0, 2, 4, 6, ...)
2i+1 = odd dimensions (1, 3, 5, 7, ...)
Sentence: "Ali killed the lion"
Let's compute positional encoding for position 0 (word "Ali"):
d_model = 6
pos = 0
# For each dimension i = 0, 1, 2 (covers dims 0-5)
i = 0:
PE(0, 0) = sin(0 / 10000^(0/6)) = sin(0) = 0.00
PE(0, 1) = cos(0 / 10000^(0/6)) = cos(0) = 1.00
i = 1:
PE(0, 2) = sin(0 / 10000^(2/6)) = sin(0) = 0.00
PE(0, 3) = cos(0 / 10000^(2/6)) = cos(0) = 1.00
i = 2:
PE(0, 4) = sin(0 / 10000^(4/6)) = sin(0) = 0.00
PE(0, 5) = cos(0 / 10000^(4/6)) = cos(0) = 1.00
PE(pos=0) = [0, 1, 0, 1, 0, 1]For position 1 (word "killed"):
pos = 1
i = 0:
PE(1, 0) = sin(1 / 10000^0) = sin(1) ≈ 0.84
PE(1, 1) = cos(1 / 10000^0) = cos(1) ≈ 0.54
i = 1:
PE(1, 2) = sin(1 / 10000^(1/3)) = sin(1/21.54) ≈ 0.046
PE(1, 3) = cos(1 / 10000^(1/3)) = cos(1/21.54) ≈ 0.999
i = 2:
PE(1, 4) = sin(1 / 10000^(2/3)) = sin(1/464.16) ≈ 0.002
PE(1, 5) = cos(1 / 10000^(2/3)) = cos(1/464.16) ≈ 1.000
PE(pos=1) = [0.84, 0.54, 0.046, 0.999, 0.002, 1.000]For position 2 (word "the"):
pos = 2
i = 0:
PE(2, 0) = sin(2 / 10000^0) = sin(2) ≈ 0.91
PE(2, 1) = cos(2 / 10000^0) = cos(2) ≈ -0.42
i = 1:
PE(2, 2) = sin(2 / 10000^(1/3)) ≈ 0.093
PE(2, 3) = cos(2 / 10000^(1/3)) ≈ 0.996
i = 2:
PE(2, 4) = sin(2 / 10000^(2/3)) ≈ 0.004
PE(2, 5) = cos(2 / 10000^(2/3)) ≈ 1.000
PE(pos=2) = [0.91, -0.42, 0.093, 0.996, 0.004, 1.000]Each position has a unique "fingerprint"!
import numpy as np
import matplotlib.pyplot as plt
def get_positional_encoding(max_seq_len, d_model):
"""
Generate sinusoidal positional encoding.
Args:
max_seq_len: Maximum sequence length
d_model: Embedding dimension (must be even)
Returns:
pos_encoding: (max_seq_len, d_model) array
"""
# Initialize positional encoding matrix
pos_encoding = np.zeros((max_seq_len, d_model))
# Compute for each position
for pos in range(max_seq_len):
for i in range(0, d_model, 2): # Even dimensions
# Compute denominator
denom = np.power(10000, (2 * i) / d_model)
# Apply sine to even indices
pos_encoding[pos, i] = np.sin(pos / denom)
# Apply cosine to odd indices
if i + 1 < d_model:
pos_encoding[pos, i + 1] = np.cos(pos / denom)
return pos_encoding
# Example usage
max_seq_len = 50
d_model = 128
PE = get_positional_encoding(max_seq_len, d_model)
print(f"Positional Encoding shape: {PE.shape}")
print(f"\nPE for position 0:\n{PE[0][:10]}") # First 10 dims
print(f"\nPE for position 1:\n{PE[1][:10]}")
print(f"\nPE for position 2:\n{PE[2][:10]}")def visualize_positional_encoding(max_seq_len=50, d_model=128):
"""Visualize positional encoding as a heatmap."""
PE = get_positional_encoding(max_seq_len, d_model)
plt.figure(figsize=(15, 8))
plt.imshow(PE, cmap='RdBu', aspect='auto', vmin=-1, vmax=1)
plt.colorbar(label='Encoding Value')
plt.xlabel('Dimension (Depth)', fontsize=12)
plt.ylabel('Position', fontsize=12)
plt.title(f'Positional Encoding Heatmap\n(Max Seq Len={max_seq_len}, d_model={d_model})',
fontsize=14)
# Add grid
plt.grid(False)
plt.tight_layout()
plt.show()
visualize_positional_encoding(50, 128)What you'll see:
Position (Y-axis: 0-50)
│
0 ├ ▓▓▒▒░░▒▒▓▓ ... (alternating pattern)
│
10 ├ ▓▒░▒▓▓▒░▒▓ ... (slightly shifted)
│
20 ├ ▓░▒▓▓▒░▒▓▓ ... (more shift)
│
30 ├ ░▒▓▓▒░▒▓▓▒ ... (continues)
│
40 ├ ▒▓▓▒░▒▓▓▒░ ... (pattern evolves)
│
50 ├ ▓▓▒░▒▓▓▒░▒ ...
└─────────────────────
0 20 40 60 80 100 120
Dimension (Depth)
Legend: ▓=1.0, ▒=0.5, ░=0.0, (negative values similar)
Low dimensions (0-20): Fast oscillation (high frequency)
Mid dimensions (20-60): Medium oscillation
High dimensions (60-128): Slow oscillation (low frequency)
Why? Different frequencies encode position at different scales!
- Fast oscillations: distinguish nearby positions
- Slow oscillations: distinguish distant positions
Look at the pattern:
Position | Dim 0 | Dim 1 | Dim 2 | Dim 3 | Dim 4 | Dim 5 |
----------|-------|-------|-------|-------|-------|-------|
0 | 0 | 1 | 0 | 1 | 0 | 1 |
1 | 0.84 | 0.54 | 0.05 | 0.99 | 0.00 | 1.00 |
2 | 0.91 | -0.42 | 0.09 | 0.99 | 0.00 | 1.00 |
3 | 0.14 | -0.99 | 0.14 | 0.99 | 0.01 | 1.00 |
This is like BINARY encoding, but with CONTINUOUS values!
Binary (discrete):
0: 0000
1: 0001
2: 0010
3: 0011
Sinusoidal (continuous):
0: [0.00, 1.00, 0.00, 1.00, ...]
1: [0.84, 0.54, 0.05, 0.99, ...]
2: [0.91, -0.42, 0.09, 0.99, ...]
3: [0.14, -0.99, 0.14, 0.99, ...]
Unique AND gradient-friendly!
Binary representation:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
Each bit alternates at different rates:
LSB (rightmost): alternates every number (0,1,0,1,...)
2nd bit: alternates every 2 numbers (0,0,1,1,0,0,...)
3rd bit: alternates every 4 numbers
MSB: alternates slowly
Same idea, but with CONTINUOUS sine/cosine:
Dimension 0 (high freq): oscillates rapidly
Dimension 2 (med freq): oscillates moderately
Dimension 4 (low freq): oscillates slowly
Like binary, but smooth! Perfect for gradients.
What the heatmap shows:
- X-axis (Depth/Dimension): 0 to 128 dimensions
- Y-axis (Position): 0 to 50 word positions
- Colors: Red (-1) to Blue (+1)
- Pattern: Low dimensions change rapidly (like LSB), high dimensions change slowly (like MSB)
Visual:
Binary: 0 1 0 1 0 1 0 1 ... (discrete jumps)
Sinusoidal: 0.0 → 0.84 → 0.91 → 0.14 → ... (smooth curve)
Positional encoding allows the model to learn RELATIVE positions through linear transformations!
For any fixed offset k:
PE(pos + k) = Linear_Transformation(PE(pos))
Specifically:
PE(pos + k, 2i) = sin((pos+k)/10000^(2i/d))
= sin(pos/10000^(2i/d)) × cos(k/10000^(2i/d))
+ cos(pos/10000^(2i/d)) × sin(k/10000^(2i/d))
= PE(pos, 2i) × constant_1 + PE(pos, 2i+1) × constant_2
This is a LINEAR combination!
# The model can learn:
PE(pos + 3) = Matrix @ PE(pos)
# Where Matrix encodes "3 positions ahead"
# This matrix is LEARNABLE during training!The attention mechanism can compute relative distances like:
- "Is this word 2 positions before that word?"
- "Are these words adjacent?"
- "What's the distance between subject and verb?"
All through simple matrix multiplication!
Given: Words at positions 5 and 8
Without positional encoding:
Can't tell they're 3 positions apart ❌
With positional encoding:
Model learns transformation T such that:
PE(8) ≈ T × PE(5)
Where T represents "+3 positions"
The model can learn this! ✓
Reference: Linear Relationships in Positional Encoding
Why do we ADD positional encoding to embeddings instead of CONCATENATING?
# Option 1: Addition (what Transformers do)
final = word_embedding + positional_encoding
# Option 2: Concatenation (why not this?)
final = [word_embedding, positional_encoding]Embedding: 512 dimensions
PE: 512 dimensions
Addition: 512 dimensions (same!) ✓
Concatenation: 1024 dimensions (doubled!) ❌
Keeping dimensions constant simplifies architecture!
Addition allows position to MODIFY word meaning:
"bank" at position 1 (subject):
embedding + PE(1) = slightly_modified_embedding_1
"bank" at position 5 (object):
embedding + PE(5) = slightly_modified_embedding_5
Different positions → different representations
But still in same semantic space!
Because PE uses orthogonal patterns (sine/cosine),
the attention mechanism can LEARN to:
- Ignore position when needed (focus on semantics)
- Focus on position when needed (syntax)
- Use both together
Addition gives the model FLEXIBILITY!
Addition: O(d_model) operations
Concatenation: 2× the model size everywhere
Addition is much faster! ✓
The beautiful pattern showing variation in different dimensions:
What this shows:
Low Dimensions (left side - 0-20):
████░░░░████░░░░████
Rapid variation! High frequency!
Each position very different from neighbors
Good for distinguishing nearby words
High Dimensions (right side - 100-128):
████████████████████
Slow variation! Low frequency!
Similar values for nearby positions
Good for understanding general position range
Each dimension contributes to position encoding at a different "resolution"!
Interactive Visualization: Linear Relationships in Positional Encoding
Step 1: Word Embeddings (simplified to 6 dims)
e_Ali = [0.2, 0.5, 0.1, 0.8, 0.3, 0.6]
e_killed = [0.4, 0.3, 0.7, 0.2, 0.9, 0.1]
e_the = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
e_lion = [0.6, 0.4, 0.2, 0.5, 0.7, 0.3]
Step 2: Positional Encodings (d_model = 6)
PE(0) = [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
PE(1) = [0.84, 0.54, 0.05, 0.99, 0.00, 1.00]
PE(2) = [0.91, -0.42, 0.09, 0.99, 0.00, 1.00]
PE(3) = [0.14, -0.99, 0.14, 0.99, 0.01, 1.00]
Step 3: Addition (Element-wise)
# Position 0: "Ali"
Ali_final = e_Ali + PE(0)
= [0.2, 0.5, 0.1, 0.8, 0.3, 0.6] + [0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
= [0.20, 1.50, 0.10, 1.80, 0.30, 1.60]
# Position 1: "killed"
killed_final = e_killed + PE(1)
= [0.4, 0.3, 0.7, 0.2, 0.9, 0.1] + [0.84, 0.54, 0.05, 0.99, 0.00, 1.00]
= [1.24, 0.84, 0.75, 1.19, 0.90, 1.10]
# Position 2: "the"
the_final = e_the + PE(2)
= [0.1, 0.1, 0.1, 0.1, 0.1, 0.1] + [0.91, -0.42, 0.09, 0.99, 0.00, 1.00]
= [1.01, -0.32, 0.19, 1.09, 0.10, 1.10]
# Position 3: "lion"
lion_final = e_lion + PE(3)
= [0.6, 0.4, 0.2, 0.5, 0.7, 0.3] + [0.14, -0.99, 0.14, 0.99, 0.01, 1.00]
= [0.74, -0.59, 0.34, 1.49, 0.71, 1.30]Now each word has BOTH semantic meaning AND position information!
-
Problem: Self-attention is position-blind
- "Ali killed lion" = "Lion killed Ali" ❌
-
Naive solutions fail:
- Simple counting: unbounded, discrete
- Normalization: inconsistent across sentences
-
Sinusoidal encoding wins:
- Bounded: [-1, 1]
- Continuous: smooth gradients
- Unique: each position different
- Relative: model can learn distances
-
Formula:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model)) PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model)) -
Binary in continuous space:
- Like binary encoding, but smooth
- Different dimensions = different frequencies
-
Addition, not concatenation:
- Preserves dimensions
- Allows fusion of semantic + positional
- Computationally efficient
-
Mind-blowing property:
- PE(pos + k) = Linear_Transform(PE(pos))
- Model can LEARN relative positions!
Now we have all the pieces:
- ✅ Self-Attention
- ✅ Scaled Dot-Product
- ✅ Multi-Head Attention
- ✅ Positional Encoding
Next up: Putting it all together in the Encoder and Decoder!
👉 Continue to: 06-encoder-decoder-architecture.md
🔗 Positional Encoding Interactive Visualizer
- Beautiful depth vs position heatmaps
- Shows variation in low vs high dimensions
- Interactive exploration
- Mathematical proofs
def get_positional_encoding(max_seq_len, d_model):
"""Generate sinusoidal positional encoding."""
PE = np.zeros((max_seq_len, d_model))
for pos in range(max_seq_len):
for i in range(0, d_model, 2):
denom = np.power(10000, (2 * i) / d_model)
PE[pos, i] = np.sin(pos / denom)
if i + 1 < d_model:
PE[pos, i + 1] = np.cos(pos / denom)
return PE
# Usage
max_len = 512
d_model = 512
PE = get_positional_encoding(max_len, d_model)
# Add to embeddings
final_embeddings = word_embeddings + PE[:seq_length]That's Positional Encoding demystified! 🎉
🔗 Transformer Explainer (Interactive Visualization)
🔗 Single vs Multi-Head Attention (ByHand.ai)
🔗 Self-Attention vs Cross-Attention (ByHand.ai)


