1 The problem RNNs have
- RNNs process one token at a time → cannot parallelize across the sequence.
- Even LSTMs struggle beyond ~200 tokens (long-range info fades).
- Attention helped, but earlier it was still bolted on top of RNNs.
- The question: "What if we used ONLY attention?" → Vaswani et al., 2017, "Attention Is All You Need."
2 Self-attention — the core idea
- Self-attention: every token attends to every other token simultaneously — all positions computed in parallel, no sequential chain.
- Each token asks: "which other tokens are relevant to me?"
- RNN: token
nmust wait for tokens1…n−1. Self-attention: all tokens at once. - Example — "The animal didn't cross the street because it was too tired." → "it" attends most strongly to "animal" (high weight). Patterns are learned from data, not hand-coded coreference.
3 Queries, Keys, Values
- Each token embedding
xᵢis projected into three vectors via separate learned weight matrices: - Query (q) — "What am I looking for?" · Key (k) — "What do I offer?" · Value (v) — "What info do I carry?"
qᵢ = W_Q · xᵢ kᵢ = W_K · xᵢ vᵢ = W_V · xᵢ
- Library analogy: query = your question, keys = book titles, values = book contents. Match query→titles, read the best-matching content.
- Q, K, V come from the same input but different matrices
W_Q, W_K, W_V— that's what makes it "self".
4 Scaled dot-product attention
Attention(Q, K, V) = softmax( QKᵀ / √dₖ ) V
- 1. QKᵀ — pairwise similarity scores (dot products between every query and key).
- 2. ÷ √dₖ — scale to prevent softmax saturation.
- 3. softmax — normalize each row into a probability distribution (weights sum to 1).
- 4. × V — weighted combination of value vectors. "Match queries to keys, then read the corresponding values."
Shapes (n tokens, dₖ = dᵥ = 4): Q is n×dₖ; QKᵀ → n×n score matrix; softmax each row; ×V → n×dᵥ output. (e.g. 3×4 · 4×3 = 3×3 scores → ·3×4 = 3×4 output.)
softmax(QKᵀ/√dₖ)V is the single most testable fact. QKᵀ = similarity, √dₖ = scaling, softmax = probabilities, V = weighted read-out. Output has the same length n as input but dim dᵥ.
5 Why divide by √dₖ
- Without scaling: dot products grow proportionally to
dₖ→ large values make softmax very "peaked" → gradients near zero (saturation). - With √dₖ scaling: normalizes variance back to ≈ 1 → softmax stays in a learnable regime → healthy gradient flow.
- Same principle as Xavier initialization: control the scale of activations to keep gradients healthy.
6 Multi-head attention
- A single head learns one type of relationship; language has many (syntax, semantics, coreference, proximity).
- Solution: run
hattention heads in parallel, each with its own smallerW_Q, W_K, W_V. - How it works: (1) Split dₘₒдₑₗ into h heads,
dₖ = dₘₒдₑₗ / h; (2) each head attends independently; (3) concatenate all head outputs; (4) project withW_Oback to dₘₒдₑₗ. - Example: 8 heads × 64 dims = 512 total → same parameter count / total compute as one big 512-dim head, but 8 different "perspectives."
- Specialization emerges from training: e.g. head 1 = subject–verb agreement, head 2 = adjective–noun proximity, head 3 = pronoun→antecedent, head 4 = punctuation boundaries.
7 Positional encoding
- The position problem: self-attention is permutation-invariant — it treats the input as a set. Without position info, "Dog bites man" = "Man bites dog." RNNs got order for free; transformers must explicitly inject it.
- Sinusoidal encoding: add a position-specific vector to each token embedding.
PE(pos, 2i) = sin( pos / 10000^(2i/d) ) PE(pos, 2i+1) = cos( pos / 10000^(2i/d) )
- Each position gets a unique "fingerprint"; relative positions have consistent patterns; generalizes to sequences longer than seen in training.
- Different frequencies per dimension (low-freq early dims, high-freq later dims). Nearby positions → similar encodings (high dot product); distant → different. Model learns to extract relative position from these signals.
8 Encoder & decoder blocks
Encoder block (stack N = 6):
- Multi-head self-attention → Add & LayerNorm → Feed-Forward Network → Add & LayerNorm.
- Residual connections around each sub-layer (same gradient-flow reason as ResNet).
Decoder block (three sub-layers, not two):
- Masked self-attention — can't see future tokens. Set future positions to
−∞before softmax → softmax(−∞)=0. Position 5 cannot peek at position 6. - Cross-attention — decoder provides queries; encoder output provides keys & values. "Decoder asks questions, encoder has answers."
- Feed-Forward Network — per-position transformation.
Full architecture: Encoder (×6, input + pos. enc.) → passes K,V → Decoder (×6, masked + cross + FFN, output + pos. enc.) → Linear + Softmax. Encoder reads input; decoder generates output one token at a time.
9 Why transformers beat RNNs (complexity)
| RNN | LSTM | Transformer | |
|---|---|---|---|
| Parallelizable | ✗ | ✗ | ✓ |
| Path length (any 2 tokens) | O(n) | O(n) | O(1) |
| Scales with compute | Limited | Limited | Excellent |
| Long-range dependencies | Poor | Good | Excellent |
| Memory (attention) | O(1) | O(1) | O(n²) |
- Self-attention complexity:
O(n²·d)time. Per-layer: self-attentionO(n²d)vs recurrentO(nd²)→ attention cheaper when n < d, costlier for very long sequences. - n=512 manageable (standard BERT); n=2048 (GPT-3 context); n=10000+ problematic → motivates efficient-attention research.
- Trade-off: O(n²) memory, but GPUs handle it for typical lengths — RNN's n sequential steps are slow on GPU, transformer is 1 parallel step. Parallelism wins.
- Original "Attention Is All You Need" base model: 65M params, 8 GPUs, ~12 hours (2017).
★ Likely exam questions
Q1. Why divide QKᵀ by √dₖ before softmax?
Large dₖ makes dot products grow, pushing softmax into saturation (near-zero gradients). Dividing by √dₖ keeps variance ≈ 1 → well-behaved gradients (same idea as Xavier init).
Q2. With 8 heads and dₘₒдₑₗ = 512, what is dₖ per head?
dₖ = dₘₒдₑₗ / h = 512 / 8 = 64. Each head works in a 64-dim subspace; concatenation returns to 512.
Q3. What happens if you remove positional encoding?
Self-attention is permutation-invariant, so the model treats "dog bites man" and "man bites dog" identically — word order is lost.
Q4. Why does the decoder use masked self-attention but the encoder does not?
The decoder is autoregressive and must not see future tokens — masking sets future positions to −∞ before softmax. The encoder sees the full input, so no mask is needed.
Q5. Complexity of self-attention vs a recurrent layer (length n, dim d)?
Self-attention: O(n²·d); recurrent: O(n·d²). Attention is cheaper when n < d, costlier for very long sequences. Self-attention time w.r.t. sequence length is O(n²).
Q6. Name one advantage and one disadvantage of transformers vs LSTMs.
Advantage: fully parallelizable with O(1) path length between any two tokens. Disadvantage: O(n²) attention memory, which becomes a problem for very long sequences.
Q7. Where do Q, K, V come from, and what does QKᵀ compute?
From the same input via different learned matrices W_Q, W_K, W_V. QKᵀ computes pairwise similarity between all token pairs; softmax normalizes it into attention weights for a weighted average of V.