Phase 4 of 7 · 14 topics
Transformers and LLMs
Build a small GPT from scratch: tokenizer, attention, training and fine-tuning. This is how modern AI works inside.
An LLM is a deep neural network with one special layer (attention), trained to guess the next token on a huge amount of text. Everything else is detail, but important detail.
Core resources for this phase
-
Video
Karpathy: Let’s build GPT, the GPT tokenizer, and reproduce GPT-2 Free
The last videos of Zero to Hero. Build along, line by line.
-
Book + code
Build a Large Language Model (From Scratch), Sebastian Raschka
Step by step in PyTorch. The code and videos are free; the book is paid.
-
Video
3Blue1Brown: transformers and attention Free
Optional. The later chapters of the neural networks series: attention and GPTs as moving pictures.
Topics, in order
-
4.1 Tokenisation and byte-pair encoding (BPE)
-
Watch
Andrej Karpathy
-
Interactive
interactive · type any text
Worth it: Type any sentence, number or emoji and instantly see how GPT-style tokenizers split it, colour-coded, with the token count. It makes tokens concrete in seconds.
-
Read
Byte-Pair Encoding tokenization
Hugging Face LLM Course
-
Code
Build a Large Language Model (From Scratch), Ch 2 (working with text data)
Sebastian Raschka · code free
-
Watch
-
4.2 Token embeddings and positional encoding
- Read
-
Read
Rotary Embeddings: A Relative Revolution
EleutherAI blog
-
Code
Build a Large Language Model (From Scratch), Ch 2 (token and positional embeddings)
Sebastian Raschka · code free
-
4.3 Self-attention: queries, keys and values
-
Watch
Attention in transformers, step-by-step
3Blue1Brown
Worth it: The clearest moving-picture explanation of queries, keys and values anywhere: you watch word meanings shift as attention mixes them.
-
Interactive
Georgia Tech Polo Club · a live GPT-2 in your browser
-
Read
Jay Alammar
-
Code
Build a Large Language Model (From Scratch), Ch 3 (coding attention mechanisms)
Sebastian Raschka · code free
-
Watch
-
4.4 Multi-head attention and the transformer block
-
Watch
Let's build GPT: from scratch, in code, spelled out
Andrej Karpathy
-
Interactive
Georgia Tech Polo Club · a live GPT-2 in your browser
Worth it: Type your own text and click through a real GPT-2: every head, every block, every probability, animated. The best way to see a transformer block from the inside.
-
Interactive
Brendan Bycroft · a 3-D walk through a GPT
-
Read
Harvard NLP
-
Code
Build a Large Language Model (From Scratch), Ch 4 (implementing a GPT model)
Sebastian Raschka · code free
-
Watch
-
4.5 GPT-style models and next-token prediction
-
Watch
Transformers, the tech behind LLMs (But what is a GPT?)
3Blue1Brown
Worth it: A visual tour of a GPT from tokens to probabilities, with the shapes of every step drawn out. The best single overview of how these models work.
-
Interactive
Brendan Bycroft · a 3-D walk through a GPT
-
Read
Jay Alammar
-
Code
Build a Large Language Model (From Scratch), Ch 5 (pretraining on unlabelled data)
Sebastian Raschka · code free
-
Watch
-
4.6 Encoder models (BERT) vs decoder models (GPT)
-
Watch
Decoder-Only Transformers, ChatGPT's specific Transformer, Clearly Explained
StatQuest (Josh Starmer)
-
Read
The Illustrated BERT, ELMo, and co.
Jay Alammar
-
Paper
Devlin et al. · arXiv · optional
-
Watch
-
4.7 Vision transformers and CLIP
-
Watch
Vision Transformer explained, with code
Umar Jamil
-
Read
Understanding Deep Learning, Ch 12 (transformers for images)
Simon Prince · free
-
Code
GitHub
-
Paper
An Image is Worth 16x16 Words (ViT, 2020)
Dosovitskiy et al. · arXiv · optional
-
Paper
Learning Transferable Visual Models From Natural Language Supervision (CLIP, 2021)
Radford et al. · arXiv · optional
-
Watch
-
4.8 Sampling: greedy, temperature, top-k, top-p
-
Read
How to generate text: using different decoding methods
Hugging Face blog
-
Code
Build a Large Language Model (From Scratch), Ch 5 (decoding strategies)
Sebastian Raschka · code free
-
Paper
The Curious Case of Neural Text Degeneration (2019)
Holtzman et al. · arXiv · optional
-
Read
-
4.9 KV cache and the cost of generation
- Watch
-
Read
Understanding and Coding the KV Cache in LLMs from Scratch
Sebastian Raschka
-
4.10 Pretraining data and scaling laws
-
Watch
Deep Dive into LLMs like ChatGPT (the pretraining part)
Andrej Karpathy
- Read
-
Paper
Training Compute-Optimal Large Language Models (Chinchilla, 2022)
Hoffmann et al. · arXiv · optional
-
Watch
-
4.11 Mixture of experts
-
Read
A Visual Guide to Mixture of Experts
Maarten Grootendorst · dozens of diagrams
Worth it: Colourful step-by-step diagrams of routers, experts, top-k selection and load balancing. Makes MoE intuitive in one read.
-
Read
Hugging Face blog
-
Paper
Outrageously Large Neural Networks: the Sparsely-Gated MoE Layer (2017)
Shazeer et al. · arXiv · optional
-
Paper
Jiang et al. · arXiv · optional
-
Read
-
4.12 Fine-tuning and LoRA
-
Watch
Umar Jamil
-
Code
Build a Large Language Model (From Scratch), Appendix E (LoRA)
Sebastian Raschka · code free
-
Paper
LoRA: Low-Rank Adaptation of Large Language Models (2021)
Hu et al. · arXiv · optional
-
Watch
-
4.13 Instruction tuning and preference tuning (RLHF, DPO)
-
Watch
Deep Dive into LLMs like ChatGPT
Andrej Karpathy
Worth it: A general-audience tour of the whole pipeline — pretraining, SFT, RLHF, reasoning — by someone who built these systems. Long, but the best big-picture view there is.
-
Read
Illustrating Reinforcement Learning from Human Feedback (RLHF)
Hugging Face blog
-
Code
Build a Large Language Model (From Scratch), Ch 7 (fine-tuning to follow instructions)
Sebastian Raschka · code free
-
Paper
Direct Preference Optimization (DPO, 2023)
Rafailov et al. · arXiv · optional
-
Watch
-
4.14 Reasoning models and test-time compute
- Watch
-
Read
Sebastian Raschka
-
Paper
Chain-of-Thought Prompting Elicits Reasoning in Large Language Models (2022)
Wei et al. · arXiv · optional
-
Paper
Self-Consistency Improves Chain of Thought Reasoning (2022)
Wang et al. · arXiv · optional
-
Paper
DeepSeek-R1: Incentivizing Reasoning Capability via RL (2025)
DeepSeek-AI · arXiv · optional
Go deeper (optional)
-
Blog
The Illustrated Transformer (Jay Alammar) Free
Every step of a transformer drawn as a picture.
-
Course
Stanford CS224n: NLP with Deep Learning Free
The full university course, for depth.
-
Paper
Attention Is All You Need (2017) Free
The original transformer paper. Read it after you have built one.
-
Paper
LoRA (2021) Free
The paper behind cheap fine-tuning.
-
Video
Umar Jamil Free
Long videos that code models from scratch and explain the maths.