Build and Train Your Own GPT-2 Model

Updated 24 days ago

This course walks through every component of GPT-2, embeddings, causal attention, residuals, layer norm, feed-forward blocks, and assembles them into a decoder-only transformer trained end-to-end.

GPT-2: The Model That Was Too Dangerous to Release

Course mapHover any lesson to see why it matters

Prerequisites

Attention Is All You Need

All transformer components: embeddings, attention, layer norm, FFN, encoder/decoder stacks

Deep Sequence Modelling — RNN

Sequence generation, next-token prediction, loss over time steps

Lessons

01Intermediate

Problem with RNNs and LSTMs

RNNs cannot scale to billion-token contexts

02Beginner

Token embeddings

Vocabulary IDs → learned d-model-dimensional vectors

03Intermediate

Positional embeddings

Add sinusoidal position codes so attention knows token order

04Intermediate

Attention & multi-head attention

12 parallel heads, each 64-dimensional, learning different patterns

05Intermediate

Causal masking

Upper-triangular mask forces left-to-right generation

06Intermediate

Residual connections

Add layer input to output — gradients flow through 12 stacked blocks

07Intermediate

Layer normalization

Pre-norm per token stabilises activations through 12 layers

08Intermediate

Feed-forward neural networks

Expand 4× to 3072, apply GELU, contract back to 768

09Intermediate

Generation of next tokens

Sequence → logits → sample: greedy, top-k, top-p, temperature

10Advanced

Decoder-only transformer

Stack 12 masked-attention + FFN blocks — no encoder needed

Unlocks

Fine-tuning GPT-2 on custom data

Adapt pretrained weights to your domain or task with SFT

RLHF & alignment

Reward modelling and PPO to align generation with human preferences

Inference optimisation & deployment

KV-cache, quantisation, and serving LLMs at scale

Test your understanding

Prof is ready

Prof will ask you questions about Build and Train Your Own GPT-2 Model — not explain it. You'll be surprised what you don't know until you have to say it.