Transformers: Attention is All You Need

Revolutionizing Sequence Modeling

$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$
Key Innovation: Self-attention mechanism enables parallel processing of sequences
Revolutionary Insight: You don't need recurrence or convolution—attention alone is sufficient for sequence modeling

Problems with RNNs and CNNs

Why We Needed Something Better

RNN/LSTM Limitations:
CNN Limitations for Sequences:
Transformer Solution: Direct connections between any two positions with self-attention

The Attention Mechanism

Query, Key, Value Paradigm

Intuition: Like searching a database with query-key matching
$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$
Attention Weight = Query · Key
Output = Σ (Attention Weight × Value)
Scaling Factor $\sqrt{d_k}$: Prevents softmax from saturating when $d_k$ is large

Self-Attention

Tokens Attending to Themselves

Key Idea: Each token in a sequence attends to all other tokens (including itself)
Input: "The cat sat on the mat"

Self-attention allows:
• "cat" to attend to "sat" (subject-verb relationship)
• "sat" to attend to "mat" (verb-object relationship)
• "the" to attend to "cat" and "mat" (determiner-noun)
• All tokens to build contextual representations
$$Q = XW_Q, \quad K = XW_K, \quad V = XW_V$$ $$\text{where } X \text{ is the input sequence}$$
Complexity: $O(n^2 \cdot d)$ where $n$ is sequence length, $d$ is dimension

Multi-Head Attention

Multiple Attention Mechanisms in Parallel

Motivation: Different heads can capture different types of relationships
$$\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, ..., \text{head}_h)W^O$$ $$\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)$$
Why Multiple Heads?
Typical Configuration: 8-16 heads, $d_{model} = 512$, $d_k = d_v = 64$

The Transformer Block

Complete Layer Architecture

Input

Multi-Head Self-Attention

Add & Norm (Residual Connection + Layer Norm)

Feed-Forward Network

Add & Norm (Residual Connection + Layer Norm)

Output
Key Components:
$$\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2$$ $$\text{LayerNorm}(x + \text{Sublayer}(x))$$
Stacking: Typically 6-12 transformer blocks for encoder, 6-12 for decoder

Positional Encoding

Adding Position Information

Problem: Self-attention is permutation invariant—no inherent position information
Solution: Add positional encodings to input embeddings
$$PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{model}}}\right)$$ $$PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{model}}}\right)$$
Sinusoidal Encoding Properties:
Final Input = Token Embedding + Positional Encoding
Both have same dimension $d_{model}$, so they can be added
Alternative: Learned positional embeddings (used in BERT, GPT)

Encoder-Decoder Architecture

Original Transformer Design

Use Case: Machine translation (e.g., English → French)
Encoder Stack (6 layers):
• Processes input sequence (English)
• Self-attention looks at all input tokens
• Produces contextual representations

Decoder Stack (6 layers):
• Generates output sequence (French)
• Masked self-attention (can't see future)
• Cross-attention to encoder output
• Autoregressive generation
Component Encoder Decoder
Self-Attention Full attention (bidirectional) Masked attention (causal)
Cross-Attention None Attends to encoder output
Input Source sequence Target sequence (shifted)
Output Contextual representations Next token probabilities

Modern Transformer Variants

Evolution Beyond the Original

BERT (2018)

Encoder-only
Bidirectional training
Masked language modeling
Classification tasks

GPT (2018)

Decoder-only
Autoregressive generation
Causal language modeling
Text generation

T5 (2019)

Encoder-decoder
Text-to-text framework
All tasks as generation
Unified approach

Vision Transformer (2020)

Encoder-only
Image patches as tokens
Self-attention for vision
Beyond NLP applications

Key Innovations by Model:
Modern Trend: Larger models (GPT-3: 175B parameters, GPT-4: ~1.7T parameters)

Training and Optimization

Making Transformers Work at Scale

Pre-training Objectives:
Optimization Challenges:
Solutions:
$$\text{Training Cost} \propto \text{Parameters} \times \text{Data} \times \text{Compute}$$

Key Takeaways

Why Transformers Changed Everything

Impact: Transformers are the foundation of modern AI—from ChatGPT to DALL-E to AlphaFold
$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$
This simple equation revolutionized artificial intelligence
Future Directions: Efficient attention mechanisms, multimodal transformers, reasoning capabilities
Slide 1 of 11