Attention Fundamentals
Attention is a mechanism that allows neural networks to dynamically focus on relevant parts of an input sequence, overcoming the fixed-capacity bottleneck of recurrent architectures.
The Problem: RNN Bottleneck
Vanishing Gradients in Long Sequences
Recurrent Neural Networks (RNNs) process sequences sequentially, maintaining a hidden state that passes information from one timestep to the next. However, this sequential processing creates a critical problem: when backpropagating gradients through many timesteps, the gradients exponentially decay or explode.
Consider a sequence of length
If each Jacobian matrix
The practical consequence: RNNs struggle to learn long-range dependencies in text. Early tokens in a long sentence have minimal influence on later tokens because gradient signals decay exponentially over time steps.
Information Compression Problem
Beyond gradients, RNNs face an architectural limitation: all information from a sequence of length
Consider translating a sentence with an RNN encoder-decoder model:
- The encoder processes the entire source sentence and outputs a single context vector
- The decoder receives this single vector to generate all target words
For long sentences (20+ words), this single vector must preserve all semantic, syntactic, and lexical information. In practice, the model forgets early words or rare details that appear early in sequences.
This architectural constraint makes RNNs fundamentally inefficient for capturing complex dependencies across long ranges.
The Solution: Attention
The Key Insight
Rather than forcing all information through a fixed bottleneck, what if the decoder could look back at all encoder states, not just the final one? The attention mechanism enables exactly this.
The fundamental idea: Use the decoder's current state to compute a weighted average of all encoder states. The weights are learned dynamically—the model learns which input positions are relevant for generating the current output token.
For a decoder state
where
This simple change has profound implications:
- No information bottleneck: Each decoder step attends directly to all encoder states
- Long-range dependencies: Direct connections bypass the sequential bottleneck
- Interpretability: Attention weights show which input words influenced each output word
Historical Context: Bahdanau vs. Luong
Two seminal approaches emerged in early attention research:
Bahdanau Attention (2015): Uses an additive scoring function (also called concat-based attention):
This approach computes an intermediate representation by concatenating and transforming the query and key, then using a learned weight vector
Luong Attention (2015): Uses a multiplicative (dot-product) scoring function:
This is simpler and more computationally efficient. Luong attention later evolved into the scaled dot-product attention used in Transformers.
Both approaches produce nearly identical results in practice, but multiplicative attention became standard due to its computational efficiency and natural compatibility with matrix operations on GPUs.
Basic Attention Formula
The Attention Mechanism Deconstructed
The attention mechanism consists of three conceptual components, each serving a specific purpose:
1. Scoring Function (How relevant is this input position?)
Given a query
| Scoring Method | Formula | Complexity |
|---|---|---|
| Dot-Product | ||
| Scaled Dot-Product | ||
| Additive | ||
| Bilinear |
The dot-product scoring is used in Transformers. The scaling factor

2. Softmax Normalization (Converting scores to probabilities)
Raw scores are often large or negative. We normalize them using softmax to create a probability distribution:
The softmax function ensures:
- All weights are positive:
- Weights sum to 1:
- Differentiable: Gradients flow during backpropagation
- Sharp focus: High-scoring positions receive exponentially more weight than low-scoring ones
Softmax is a "soft" version of argmax (hard selection), allowing the model to learn a smooth, differentiable attention mechanism.

3. Context Vector Computation (Weighted aggregation)
The normalized weights multiply the values (another projection of encoder states):
where


Complete Attention Computation
Combining these steps in matrix form:
Here:
is a matrix of queries (decoder states), shape is a matrix of keys (encoder states), shape is a matrix of values (encoder states), shape - Output shape:
This formula is the foundation for all attention mechanisms, including multi-head attention (Module 3) and self-attention in Transformers (Module 2).

Practical Insight: Attention weight values are interpretable. In machine translation, an attention weight of 0.8 on the 3rd input word means the model is allocating 80% of focus to generating the current output word based on that input word. This interpretability is one reason Transformers became popular in industry.


Interview Questions
Question 1: Why does attention solve the vanishing gradient problem?
Sample Answer:
Attention creates direct connections from the decoder to all encoder positions, bypassing the sequential bottleneck of RNNs.
With vanilla RNNs, the gradient path from output word
With attention, any decoder state can directly attend to any encoder state through a single attention computation. The gradient path becomes:
Specifically, the loss gradient flows directly through the attention weights
However, we must be precise: attention doesn't eliminate gradient issues entirely. Softmax normalization and subsequent projections still require proper scaling (e.g., the
Follow-up consideration: LSTM and GRU gates also address vanishing gradients through architectural gating. Attention complements these approaches by adding long-range connectivity.
Question 2: Explain the difference between additive (Bahdanau) and multiplicative (Luong/dot-product) attention. When would you use each?
Sample Answer:
Additive Attention (Bahdanau):
- Scoring function:
- Computes a learned transformation of the concatenated query and key
- Requires two weight matrices (
, ) and a weight vector ( ) - More parameters and higher computational cost:
- Can capture more complex interactions between query and key
Multiplicative Attention (Luong/Dot-Product):
- Scoring function:
(bilinear) or (dot-product) - Requires fewer parameters (one weight matrix or none)
- Computational cost:
for dot-product, for bilinear - More efficient, especially for large embedding dimensions
- Naturally parallelizable on modern hardware (GPUs, TPUs)
When to use each:
Use additive attention when:
- Computational resources are not constrained
- You have smaller embedding dimensions (
) - You need maximum expressiveness for complex alignment patterns
- You're working in an academic setting where efficiency isn't critical
Use multiplicative attention when:
- You're building production systems with high throughput requirements
- Embedding dimensions are large (
) - You want to leverage GPU/TPU acceleration
- You prioritize computational efficiency
- You're implementing Transformers (the standard choice)
Empirical note: Modern research shows dot-product attention with proper scaling (
Question 3: How does attention handle variable-length sequences and padding? Why is masking important?
Sample Answer:
In practice, sequences in a batch have different lengths. For computational efficiency, we pad shorter sequences to match the longest sequence in the batch, introducing padding tokens.
The Problem with Naïve Attention:
Without masking, the attention mechanism will compute weights for padding tokens:
Since softmax normalizes across all positions, including padding, the model can attend to padding tokens. This is problematic because:
- Padding tokens contain no semantic information
- The model wastes attention capacity on meaningless positions
- Gradients flow to and from padding positions, wasting computation
The Solution: Masking
Before applying softmax, we set the scores of padding positions to
This ensures
In implementation:
scores = Q @ K.T / sqrt(d_k)
scores[padding_mask] = -1e9 # Large negative number approximates -∞
attention_weights = softmax(scores)
context = attention_weights @ VOther Masking Scenarios:
- Decoder self-attention in autoregressive models: Mask future positions to prevent cheating. A decoder should only attend to previously generated tokens.
- Causal masking: Upper triangular masking ensures position
can only attend to positions .
Practical Impact: Without proper masking, model predictions are undefined on sequences with different lengths, and the model incorrectly learns from padding. With masking, the model becomes robust to variable-length inputs and focuses only on meaningful tokens.
Quick Reference Card
╔════════════════════════════════════════════════════════════════════════════╗
║ ATTENTION FUNDAMENTALS CHEAT SHEET ║
╚════════════════════════════════════════════════════════════════════════════╝
┌─ PROBLEM: RNN BOTTLENECK ────────────────────────────────────────────────┐
│ │
│ Vanishing Gradients: Gradient ≈ ∏(Jacobians) → decays exponentially │
│ Information Bottleneck: All info compressed into single vector h_T │
│ Sequential Processing: Can't parallelize, O(T) depth │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ ATTENTION EQUATION ─────────────────────────────────────────────────────┐
│ │
│ Attention(Q, K, V) = softmax(QK^T / √d_k) V │
│ │
│ Q: Queries (what to look for) [batch_size, seq_len, d_k] │
│ K: Keys (what can be found) [batch_size, seq_len, d_k] │
│ V: Values (information to take) [batch_size, seq_len, d_v] │
│ d_k: Key dimension │
│ │
│ Output: Context vectors [batch_size, seq_len, d_v] │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ SCORING FUNCTIONS ──────────────────────────────────────────────────────┐
│ │
│ Name Formula Complexity Best For │
│ ───────────────────────────────────────────────────────────────────── │
│ Dot-Product q · k O(d) Large d │
│ Scaled DP q · k / √d_k O(d) Transformers│
│ Additive v^T tanh(W_q q + W_k k) O(d²) Small d │
│ Bilinear q^T W k O(d²) Flexibility │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ SOFTMAX NORMALIZATION ──────────────────────────────────────────────────┐
│ │
│ Purpose: Convert scores to probability distribution [0, 1] │
│ Formula: α_i = exp(score_i) / Σ_j exp(score_j) │
│ Property: Σ_i α_i = 1 │
│ Effect: High scores → exponentially higher weights │
│ Focuses on top few positions │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ CONTEXT VECTOR ─────────────────────────────────────────────────────────┐
│ │
│ Weighted aggregation of values: c = Σ_i α_i v_i │
│ │
│ Intuition: Each output position gets a blend of input information, │
│ weighted by how relevant each input is. │
│ │
│ Parallelizable: All positions computed simultaneously (O(1) depth) │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ KEY IMPROVEMENTS OVER RNNs ─────────────────────────────────────────────┐
│ │
│ ✓ Direct connections bypass sequential bottleneck │
│ ✓ Path length: O(1) instead of O(T) │
│ ✓ Fully parallelizable across sequence │
│ ✓ Interpretable: attention weights show importance │
│ ✓ No information compression: attend to all positions │
│ ✓ Gradient flow: direct paths prevent vanishing gradients │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ MASKING STRATEGIES ─────────────────────────────────────────────────────┐
│ │
│ Padding Mask: Set scores of <pad> tokens to -∞ │
│ Prevents attending to padding │
│ │
│ Causal Mask: Upper triangular: position t sees only t ≤ pos │
│ Required for autoregressive models │
│ │
│ Implementation: scores[mask] = -1e9 (before softmax) │
│ Ensures α_masked ≈ 0 after softmax │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ HISTORICAL CONTEXT ─────────────────────────────────────────────────────┐
│ │
│ 2014-2015: Bahdanau et al., Luong et al. │
│ RNN + Attention for machine translation │
│ │
│ 2017: Vaswani et al. │
│ "Attention Is All You Need" │
│ Scaled dot-product attention │
│ Multi-head attention │
│ Transformer architecture │
│ │
└────────────────────────────────────────────────────────────────────────────┘
┌─ CONNECTING TO OTHER MODULES ────────────────────────────────────────────┐
│ │
│ Module 1 (Current): Single attention head, foundational concepts │
│ Module 2: Self-attention: Q, K, V all from same source │
│ Module 3: Multi-head attention: Parallel attention mechanisms │
│ Module 4: Positional encoding: Inject sequence position info │
│ Module 5: Full Transformer: Combine all components │
│ │
└────────────────────────────────────────────────────────────────────────────┘Visual Cheat Sheet
Key Takeaways
Attention solves the RNN bottleneck by providing direct connections between distant positions, enabling parallel computation and preventing vanishing gradients.
The attention formula (Attention(Q, K, V)) is elegantly simple: compute compatibility scores between queries and keys, normalize with softmax, and aggregate values using resulting weights.
Scaled dot-product attention is the standard due to computational efficiency while maintaining expressiveness equivalent to more complex scoring functions.
Masking is critical for handling variable-length sequences and preventing the model from attending to meaningless padding or future tokens.
Attention is interpretable: The attention weights directly indicate which input positions influenced each output position, providing transparency many other neural components lack.
This module builds the foundation for self-attention (Module 2) and multi-head attention (Module 3), which are core to the Transformer architecture that has revolutionized NLP and beyond.