GPT-2: Language Models as Unsupervised Learners
GPT-2 is a large-scale autoregressive language model introduced by OpenAI in 2019 that learns to generate coherent, fluent text purely by predicting the next word in a sequence — with no task-specific training whatsoever. It matters because it demonstrated for the first time that a single large unsupervised model could perform competitively across many NLP tasks without any fine-tuning, sparking the modern era of foundation models. The counterintuitive fact: OpenAI initially refused to release the full model, calling it "too dangerous" — a decision that was both historically unprecedented and widely debated.
GPT-2's central insight is deceptively simple: if you train a large enough neural network to predict the next word in text scraped from the internet, it is forced to implicitly learn grammar, facts, reasoning, and even some task structure — because doing all of those things makes next-word prediction easier. The model never sees a label like "this is a translation task" or "this is a summary task"; instead, all task knowledge is encoded in the patterns of language itself. At inference time, you can guide the model toward a specific task just by phrasing your input in a way that resembles how that task appears in natural text — this is called zero-shot prompting. The "aha moment" is realizing that language modeling is not just a narrow NLP task but a kind of compression of human knowledge.
How It Works
GPT-2 is built on the Transformer decoder architecture. It processes a sequence of tokens (roughly, word-pieces) one at a time and at each position predicts what token comes next. The model is trained entirely on this single objective across 40GB of web text called WebText.
Tokenization
Before any math happens, raw text is converted to integers using Byte-Pair Encoding (BPE). The vocabulary has 50,257 tokens — common words get their own token, rare words are split into sub-word pieces. ```python
# Install: pip install transformers from transformers import GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained("gpt2") tokens = tokenizer.encode("The quick brown fox") print(tokens) # [464, 2068, 7586, 21831] print(len(tokens)) # 4 ```
The Language Modeling Objective
GPT-2 is trained to maximize the probability of the correct next token at every position. Across a sequence of tokens , this is written as:
Explanation: The model is penalized (via negative log probability) for every token it assigns low probability to, summed across all positions in the training text.
Symbols:
- — the loss to be minimized during training
- — total number of tokens in the sequence
- — the token at position
- — the model's predicted probability for token given all previous tokens
Masked Self-Attention
The critical mechanism that allows GPT-2 to look at all prior tokens simultaneously (rather than one at a time) is causal self-attention. For each token position, the model computes a weighted average of all previous positions' representations:
Explanation: Each token asks a "query," every earlier token offers a "key," and the similarity between query and keys determines how much each earlier token's "value" contributes to the current token's updated representation.
Symbols:
- — query matrix (what the current token is looking for)
- — key matrix (what each token offers to be found)
- — value matrix (the actual information each token contributes)
- — dimension of the key vectors; dividing by prevents the dot products from becoming too large before softmax
The word "masked" means position is blocked from attending to positions or later — the model cannot cheat by looking at future tokens during training.
Perplexity: Measuring Language Model Quality
A common evaluation metric is perplexity, which measures how surprised the model is by test text:
Explanation: Perplexity is essentially the average number of equally-likely next tokens the model is choosing between at each step — lower is better.
Symbols:
- — perplexity score (GPT-2 achieved ~18 on Penn Treebank without fine-tuning)
- The rest match Equation 1
Model Sizes
GPT-2 was released in four sizes, demonstrating that scale matters:
| Version | Layers | Heads | Parameters |
|---|---|---|---|
| Small | 12 | 12 | 117M |
| Medium | 24 | 16 | 345M |
| Large | 36 | 20 | 762M |
| XL | 48 | 25 | 1.5B |
Simple Generation in Python
```python from transformers import pipeline generator = pipeline("text-generation", model="gpt2") result = generator( "Once upon a time in a land of neural networks,", max_new_tokens=50, do_sample=True, temperature=0.8, # controls randomness; lower = more deterministic top_k=50 # only sample from top-50 likely tokens ) print(result[0]["generated_text"]) ```
Zero-Shot Task Prompting
GPT-2 showed that task format can be embedded in the prompt itself: ```python
# Translation via prompt engineering prompt = "Translate English to French:\nEnglish: Hello, how are you?\nFrench:" result = generator(prompt, max_new_tokens=20, do_sample=False) print(result[0]["generated_text"])
# GPT-2 often continues: "Bonjour, comment allez-vous?" ``` The model was never trained on translation pairs — it simply recognizes the format from web text.
Alec Radford is a researcher at OpenAI who led the development of both GPT and GPT-2, pioneering the idea that language modeling alone could serve as a universal pre-training objective for NLP. His 2019 paper "Language Models are Unsupervised Multitask Learners" introduced the zero-shot paradigm that would define the next half-decade of NLP research. Surprisingly, Radford has no formal academic PhD — he is largely self-taught and joined OpenAI in his early twenties.
Ilya Sutskever co-founded OpenAI after his landmark work on sequence-to-sequence models and deep learning at the University of Toronto under Geoffrey Hinton. While not the lead author of GPT-2, Sutskever's organizational and research vision at OpenAI directly enabled the scaling experiments that made GPT-2 possible. He was a co-inventor of the dropout regularization technique and is one of the most cited researchers in deep learning history.
Intuition
Imagine you hire someone who has read essentially the entire internet — every Wikipedia article, every Reddit thread, every news story, every book published online. You never taught them any specific skill. But when you hand them a half-finished sentence and say "keep going," they can do it remarkably well for almost any topic. Now imagine asking them to translate a sentence by writing: "English: Hello. French:" — they've seen that pattern so many times online that they just... complete it. That's GPT-2. The model is not a specialist who studied French; it's a generalist who has absorbed so much language that French translation falls out naturally. The key mental shift is that language itself encodes task structure — if you describe a task in the same format the model saw during training, it can often perform that task without ever being explicitly taught it. Think of it less like a programmed tool and more like a very well-read autocomplete that has internalized the shape of human knowledge.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
GPT-3 and Few-Shot Learning · BERT and Masked Language Modeling · Transformer Architecture · Reinforcement Learning from Human Feedback (RLHF) · Emergent Abilities in Large Language Models