What is a Transformer?
The Transformer is a neural network architecture introduced in the landmark paper "Attention Is All You Need" (Vaswani et al., 2017). It revolutionized deep learning by replacing recurrent (RNN/LSTM) and convolutional architectures with a purely attention-based mechanism, enabling efficient parallelization and superior modeling of long-range dependencies.
π‘ Key Innovation: Unlike RNNs that process sequences step-by-step, Transformers process all tokens in parallel, dramatically reducing training time and enabling models to scale to billions of parameters.
Transformers are the foundation of virtually all modern AI systems including:
OpenAI
Meta
Anthropic
High-Level Architecture
The original Transformer uses an encoder-decoder structure designed for sequence-to-sequence tasks like translation:
The original Transformer architecture from "Attention Is All You Need" (Vaswani et al., 2017)
Encoder-Only
Understanding & Classification
Examples: BERT, RoBERTa, ALBERT
Decoder-Only
Text Generation
Examples: GPT, LLaMA, Claude
Encoder-Decoder
Seq-to-Seq Tasks
Examples: T5, BART, Flan-T5
Self-Attention Mechanism
Self-attention is the core innovation of Transformers. It allows each token in a sequence to "attend to" (or consider) every other token, capturing contextual relationships regardless of distance.
How Self-Attention Works
For each token embedding, three vectors are computed via linear projections:
"What am I looking for?"
"What do I offer?"
"What's my content?"
The attention formula:
Attention(Q, K, V) = softmax(QKα΅ / βdβ) Β· V
Scaling by βdβ prevents extremely large dot products, stabilizing gradients during training.
Multi-Head Attention
Instead of a single attention operation, Transformers use multiple attention heads in parallel. Each head learns different types of relationships:
Syntax
Semantics
Position
Coreference
headα΅’ = Attention(QWα΅’α΄½, KWα΅’α΄·, VWα΅’β±½) MultiHead(Q,K,V) = Concat(headβ,...,headβ)Wα΄Ό
Positional Encoding
Since Transformers process all tokens simultaneously (no inherent order), positional information must be explicitly injected. Common approaches:
| Method | Description | Used In |
|---|---|---|
| Sinusoidal | Fixed sin/cos functions at different frequencies | Original Transformer |
| Learned Absolute | Trainable position embeddings | BERT, GPT-2 |
| Relative Position | Encodes distance between tokens | Transformer-XL, T5 |
| RoPE | Rotary Position Embeddings | LLaMA, Mistral, GPT-NeoX |
BERT vs GPT: Key Differences
| Aspect | BERT | GPT |
|---|---|---|
| Architecture | Encoder-only | Decoder-only |
| Attention | Bidirectional (sees all tokens) | Causal/Masked (left-to-right only) |
| Pre-training | Masked Language Modeling (MLM) | Next Token Prediction (autoregressive) |
| Best For | Understanding, Classification, NER | Text Generation, Conversation |
| Developer | Google (2018) | OpenAI (2018+) |
Computational Complexity
β οΈ Challenge: Standard self-attention has O(nΒ²) complexity in both time and memory, limiting context length to thousands of tokens.
This has driven research into efficient variants:
- FlashAttention: IO-aware algorithm that reduces memory reads/writes
- Sparse Attention: Attend only to specific positions (Longformer, BigBird)
- Linear Attention: Approximate attention with O(n) complexity (Performer)
- State Space Models: Alternative to attention (Mamba, S4)
- Ring Attention: Distributed attention across multiple devices
Modern Improvements (2024)
π Pre-Layer Normalization
Apply LayerNorm before attention/FFN instead of after, improving training stability.
π― Grouped-Query Attention
Share Key/Value heads across Query heads to reduce KV-cache size.
π RoPE Embeddings
Rotary embeddings enable better length generalization and efficient inference.
β‘ SwiGLU Activation
Gated linear units replacing ReLU in FFN for better performance.
Applications
ChatBots & LLMs
Search & RAG
Code Generation
Vision (ViT)
Audio & Speech
Protein Folding
Learn More
π Essential Resources
Related Topics
Test Your Knowledge
Score 8/10 or higher to pass
You need to be logged in to take this quiz.
Login to Continue