AI Notes
Master GPT architecture for AI. Learn causal language modeling, in-context learning, scaling laws, GPT-3, GPT-4. Interview guide 2024.
The Autoregressive Paradigm
GPT (Generative Pre-trained Transformer) represents a fundamentally different philosophy from BERT. Where BERT uses bidirectional context for understanding, GPT uses left-to-right (causal) attention for generation. The model predicts the next token given all previous tokens, making it naturally suited for text generation, dialogue, code completion, and any task that can be framed as sequence completion.
The GPT family—from GPT-1 (2018) through GPT-4 (2023)—demonstrated that scaling up autoregressive language models produces increasingly capable systems, eventually exhibiting emergent abilities like in-context learning, chain-of-thought reasoning, and instruction following without task-specific training.
Architecture: Decoder-Only Transformer
Structural Comparison
BERT (Encoder-only)
Bidirectional self-attention
[CLS] token for classification
Cannot generate text
GPT (Decoder-only)
Causal (masked) self-attention
Next-token prediction
Natural text generation
GPT Architecture Stack
Causal Attention Mask
The key mechanism that makes GPT autoregressive:
This ensures that during training, each position's prediction uses only past context—exactly matching the generation scenario where future tokens don't exist yet.
Training Objective
Next-Token Prediction (Causal Language Modeling)
| Training text | "The cat sat on the mat" |
| Input | [The, cat, sat, on, the] |
| Target | [cat, sat, on, the, mat] |
| (efficient | one forward pass gives N predictions for N tokens) |
Training Efficiency
Unlike BERT's MLM which only trains on 15% of tokens per example, GPT trains on every token in every sequence. This makes GPT significantly more sample-efficient per token processed, though it sees less context per prediction (only left context vs. bidirectional).
Scaling Laws: The Power of Size
Empirical Scaling Laws (Kaplan et al., 2020)
GPT Family Scaling
| GPT-1 (2018) | 117M params, 4.6GB text, BookCorpus |
| GPT-2 (2019) | 1.5B params, 40GB text, WebText |
| GPT-3 (2020) | 175B params, 570GB text, CommonCrawl mix |
| GPT-4 (2023) | ~1.8T params (est.), massive multi-modal data |
Each generation brought not just better performance on existing tasks, but qualitatively new capabilities (emergent abilities).
In-Context Learning
GPT-3's breakthrough discovery: large models can learn tasks from examples in the prompt without any parameter updates.
Few-Shot Learning
| sea otter | loutre de mer |
| cheese | fromage |
| plaid shirt | " |
| GPT-3 generates | "chemise à carreaux" |
Types of In-Context Learning
| Zero-shot | Task description only, no examples |
| "Translate to French: cheese | " |
| One-shot | One example provided |
| "sea otter | loutre de mer |
| cheese | " |
| Few-shot | Several examples (typically 3-10) |
| "sea otter | loutre de mer |
| plaid shirt | chemise à carreaux |
| cheese | " |
Performance generally improves with more examples, up to context window limits.
Generation Strategies
Temperature Sampling
| Logits | [2.0, 1.0, 0.5, 0.1] |
| Temperature T=1.0 | softmax([2.0, 1.0, 0.5, 0.1]) |
| Temperature T=0.5 | softmax([4.0, 2.0, 1.0, 0.2]) |
| Temperature T=2.0 | softmax([1.0, 0.5, 0.25, 0.05]) |
Top-k and Top-p (Nucleus) Sampling
| Top-k (k=3) | Only sample from top 3 tokens by probability |
| Top-p (p=0.9) | Sample from smallest set whose cumulative probability ≥ 0.9 |
| Greedy (T | 0): Always pick highest probability token |
From GPT-3 to InstructGPT and ChatGPT
The Alignment Problem
Raw GPT-3 is trained to predict internet text—it mimics the training distribution, which includes toxic content, hallucinations, and unhelpful patterns. Making it useful and safe requires alignment.
RLHF Pipeline (Reinforcement Learning from Human Feedback)
| Step 1 | Supervised Fine-Tuning (SFT) |
| Step 2 | Reward Model Training |
| Step 3 | PPO Optimization |
This pipeline produced InstructGPT and later ChatGPT—models that follow instructions, refuse harmful requests, and produce helpful responses.
GPT vs. BERT: When to Use Which
| Aspect | GPT | BERT |
|---|---|---|
| Architecture | Decoder-only | Encoder-only |
| Attention | Causal (left-to-right) | Bidirectional |
| Best for | Generation, dialogue | Classification, NER |
| Training | Next-token prediction | Masked LM + NSP |
| Context | Left context only | Full context |
| Fine-tuning | Optional (in-context) | Required |
Emergent Abilities
Large GPT models exhibit capabilities not present in smaller versions:
| ~10B params | Basic in-context learning |
| ~100B params | Chain-of-thought reasoning, basic arithmetic |
| ~500B+ params | Complex reasoning, code generation, |
These abilities appear suddenly at certain scales—they're not gradual improvements but phase transitions in capability.
Interview Questions
Q: Why causal attention instead of bidirectional? A: During generation, future tokens don't exist yet—you can't attend to what hasn't been produced. Causal masking at training time matches this generation constraint, ensuring consistent behavior between training and inference.
Q: How does in-context learning work mechanistically? A: The transformer's attention layers implement implicit gradient descent on the examples. The model effectively "trains" on the few-shot examples within its forward pass, using attention to implement a learning algorithm over the context.
Q: What are the limitations of autoregressive generation? A: Each token is committed once generated—no revision. The model can't "plan ahead" or consider global document structure. It's also sequential (each token depends on the previous), making generation inherently slower than parallel encoding.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for GPT Models - Generative Pre-trained Transformers.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, deep, learning, gpt, models
Related Artificial Intelligence Topics