The attention mechanism is fundamentally about letting a model decide which parts of the input to focus on when processing each element. Think of it like reading a sentence - when you encounter the word "it," you automatically look back to figure out what "it" refers to.
The Core Problem
Traditional neural networks process sequences one element at a time, from left to right. But language doesn't work that way. Consider this sentence: "The cat that was sitting on the mat was hungry." When processing "was hungry," the model needs to connect back to "cat," not just the immediately preceding words.
What Attention Does
Attention creates connections between every word and every other word in the sequence. For each word, it calculates how much it should "pay attention" to each other word, including itself.
Let's use a simple example: "The cat sat on the mat"
When processing "sat," attention might assign these weights:
- "The": 0.1 (low attention)
- "cat": 0.7 (high attention - the subject doing the sitting)
- "sat": 0.1 (some self-attention)
- "on": 0.05
- "the": 0.03
- "mat": 0.02
The Three Components: Query, Key, Value
Attention uses three vectors for each word:
Query (Q): "What am I looking for?" - represents what the current word wants to know about other words.
Key (K): "What do I represent?" - represents what information each word can provide.
Value (V): "What information do I actually contain?" - the actual content that gets passed along.
Think of it like a library system:
- Query: Your search request ("I need information about cats")
- Key: The index labels on books ("This book is about animals")
- Value: The actual content inside the books
The Attention Calculation
Here's the step-by-step process:
-
Create Q, K, V vectors: Each word gets transformed into these three representations using learned weight matrices.
-
Calculate attention scores: For each word, compute how well its Query matches with every word's Key using dot product:
Score = Query · Key
-
Apply softmax: Convert scores to probabilities that sum to 1:
Attention_weights = softmax(Scores)
-
Weighted combination: Multiply each word's Value by its attention weight and sum them up:
Output = Σ(Attention_weight × Value)
A Concrete Example
Let's say we're processing "The cat sat" and focusing on the word "sat":
-
Queries, Keys, Values (simplified as 2D vectors):
- "The": Q=[0.1, 0.2], K=[0.3, 0.1], V=[0.5, 0.8]
- "cat": Q=[0.4, 0.6], K=[0.7, 0.9], V=[0.2, 0.3]
- "sat": Q=[0.8, 0.3], K=[0.2, 0.4], V=[0.6, 0.1]
-
Calculate attention scores for "sat" (using its Query):
- Score with "The": [0.8, 0.3] · [0.3, 0.1] = 0.27
- Score with "cat": [0.8, 0.3] · [0.7, 0.9] = 0.83
- Score with "sat": [0.8, 0.3] · [0.2, 0.4] = 0.28
-
Apply softmax: [0.27, 0.83, 0.28] → [0.2, 0.6, 0.2]
-
Weighted combination:
Output = 0.2×[0.5, 0.8] + 0.6×[0.2, 0.3] + 0.2×[0.6, 0.1] = [0.34, 0.36]
The high attention weight (0.6) on "cat" means "sat" is strongly attending to "cat," which makes linguistic sense.
Multi-Head Attention
Instead of just one attention mechanism, transformers use multiple "heads" running in parallel. Each head learns to focus on different types of relationships:
- Head 1 might focus on subject-verb relationships
- Head 2 might focus on adjective-noun relationships
- Head 3 might focus on long-distance dependencies
The outputs from all heads are concatenated and transformed to create the final result.
Why This Works
Attention solves several key problems:
- Parallelization: Unlike RNNs, all positions can be computed simultaneously
- Long-range dependencies: Direct connections between distant words
- Interpretability: Attention weights show what the model is focusing on
- Flexibility: The model learns what relationships matter most
The beauty is that attention weights are learned during training. The model discovers that "sat" should pay attention to "cat" not because we programmed this rule, but because this pattern helps minimize the training loss across thousands of examples.
This mechanism, repeated across multiple layers with different learned parameters, allows transformers to build increasingly sophisticated representations of language structure and meaning.