A neural network trained to predict the next word
An LLM — large language model — is a deep neural network trained on massive amounts of text to understand, generate, and respond to human-like language. Earlier NLP methods handled narrow tasks (spam classification, simple pattern matching) but struggled with open-ended language; LLMs, powered by the transformer and vast data, are broadly capable across many tasks.
Key concept · What "large" means, and the AI hierarchy
"Large" refers to two things at once: the model's size (tens or hundreds of billions of parameters — the adjustable weights tuned during training) and the immense dataset it's trained on. LLMs sit inside a nested hierarchy: artificial intelligence ⊃ machine learning (algorithms that learn from data instead of hand-written rules) ⊃ deep learning (neural networks with many layers, no manual feature engineering) — and LLMs are a specific application of deep learning. Because they generate text, LLMs are also a form of generative AI (GenAI).
Key concept · Next-word prediction and self-supervision
An LLM is trained on a deceptively simple task: predict the next word in a sequence. This is a form of self-supervised learning — the data provides its own labels, because the next word in the text is the label the model should predict. No human labeling is needed, so the model can learn from massive unlabeled corpora. This simple objective forces the model to learn grammar, context, structure, and even world knowledge — which is why researchers were surprised it produces such capable models.
Pretraining, then finetuning
Creating an LLM has two stages. Why build your own? Coding one from scratch teaches its mechanics and limits, and custom-built LLMs can outperform general ones on specific domains (e.g. BloombergGPT for finance) — plus advantages in data privacy, on-device deployment, lower latency, and full control.
Key concept · Pretraining creates a foundation model
Pretraining is the first stage: train the model on a large, diverse corpus of raw (unlabeled) text via next-word prediction. The "pre" signals that this is the initial phase; the result is a base model or foundation model (GPT-3 is the classic example) that can already do text completion and shows few-shot capabilities — learning a new task from just a handful of examples in the prompt. Pretraining is expensive (GPT-3's was estimated at ~$4.6M in compute), so the book trains a small model for learning and also shows how to load openly available pretrained weights.
Key concept · Finetuning: instruction vs. classification
Finetuning is the second stage: further train the pretrained model on a smaller, labeled dataset for a specific purpose. The two most common kinds: instruction-finetuning, where the labeled data is (instruction, answer) pairs (e.g. a translation request and its correct translation) — this is how ChatGPT was made from GPT-3; and classification finetuning, where the data is texts with class labels (e.g. emails labeled spam / not-spam). Finetuning needs far less data and compute than pretraining because it builds on capabilities the base model already has.
The architecture behind modern LLMs
Most modern LLMs are built on the transformer, introduced in the 2017 paper Attention Is All You Need and originally designed for machine translation (English → German/French).
Key concept · Encoder, decoder, and self-attention
The original transformer has two submodules. The encoder processes the input text and turns it into numerical embedding vectors capturing its meaning; the decoder uses those to generate the output text one word at a time. The key component tying it together is self-attention, which lets the model weigh the importance of different words in a sequence relative to each other — capturing long-range dependencies and context. (Self-attention is coded from scratch in Chapter 3; data preprocessing in Chapter 2.)
Key concept · BERT vs. GPT
Two families adapted the transformer differently. BERT (Bidirectional Encoder Representations from Transformers) builds on the encoder and is trained on masked word prediction — filling in hidden words using context from both sides — making it strong at classification tasks (Twitter used it to detect toxic content). GPT (Generative Pretrained Transformer) builds on the decoder and is designed for generating text. A note on terminology: transformers and LLMs aren't synonyms — not all transformers are LLMs (some do vision), and not all LLMs are transformers (some use recurrent/convolutional architectures) — but this book uses "LLM" to mean a GPT-like transformer.
A decoder-only model — and how we'll build it
GPT stands for Generative Pretrained Transformer (Radford et al., 2018). Compared to the original encoder-decoder transformer, GPT is simpler: it's just the decoder portion, without the encoder.
Key concept · Autoregressive generation and emergent behavior
GPT is an autoregressive model: it generates one word at a time, feeding each output back as input for the next prediction, so every new word is conditioned on all the words before it. Despite training only on simple next-word prediction, GPT models show emergent behavior — abilities never explicitly taught, like translation, classification, and summarization — that arise from exposure to vast, diverse data. GPT models are also large: the original transformer repeated its blocks 6 times, while GPT-3 has 96 transformer layers and 175 billion parameters.
Key concept · The three-stage plan for the book
The rest of the book codes a GPT from scratch in three stages. Stage 1 — Building an LLM: data preparation & sampling, the attention mechanism, and the LLM architecture (Chapters 2–4). Stage 2 — Pretraining: the training loop and model evaluation, producing a foundation model, plus loading openly available pretrained weights (Chapter 5). Stage 3 — Finetuning: take the pretrained model and finetune it into a classifier (Chapter 6) or a personal assistant / instruction-follower (Chapter 7). Everything runs on consumer hardware for educational purposes.
Chapter 1 quiz
Fifteen questions on what LLMs are, pretraining/finetuning, transformers, and GPT. Answer first, then expand Show answer.
Part A · Multiple choice
The core task an LLM is trained on is:
- A Predicting the next word in a sequence
- B Classifying images
- C Sorting numbers
- D Compressing files
Show answer
A. A simple objective that yields surprisingly capable models.
Next-word prediction is a form of:
- A Self-supervised learning (the data provides its own labels)
- B Reinforcement learning
- C Manual labeling
- D Unsupervised clustering
Show answer
A. The next word is the label, so no human labeling is needed.
The "large" in large language model refers to:
- A Both the model's parameters and the size of its training dataset
- B The screen size
- C The output length
- D The vocabulary only
Show answer
A.
Which nesting is correct?
- A AI ⊃ machine learning ⊃ deep learning ⊃ LLMs
- B LLMs ⊃ AI ⊃ deep learning
- C Deep learning ⊃ AI
- D Machine learning ⊃ AI
Show answer
A. LLMs are a specific application of deep learning, and a form of generative AI.
Pretraining produces what?
- A A base / foundation model
- B A finished classifier
- C A tokenizer
- D A dataset
Show answer
A. Trained on raw unlabeled text via next-word prediction.
The two most common categories of finetuning are:
- A Instruction-finetuning and classification finetuning
- B Pretraining and tokenizing
- C Encoding and decoding
- D Zero-shot and few-shot
Show answer
A. Instruction uses (instruction, answer) pairs; classification uses labeled texts.
The transformer was introduced in which paper?
- A Attention Is All You Need (2017)
- B ImageNet (2012)
- C Word2Vec (2013)
- D AlphaGo (2016)
Show answer
A. Originally for machine translation.
In the original transformer, the encoder's job is to:
- A Process the input text into embedding vectors
- B Generate the output one word at a time
- C Tokenize the output
- D Compute the loss
Show answer
A. The decoder then generates output from those vectors.
GPT stands for:
- A Generative Pretrained Transformer
- B General Purpose Tool
- C Global Prediction Transformer
- D Gradient Propagation Technique
Show answer
A. Built on the decoder portion of the transformer.
GPT is described as autoregressive because:
- A It feeds each generated word back as input for the next prediction
- B It runs backwards
- C It needs no data
- D It uses an encoder
Show answer
A. Each new word is conditioned on all preceding words.
Part B · True or false
BERT is trained on masked word prediction and excels at classification tasks.
Show answer
True. It builds on the encoder; GPT builds on the decoder for generation.
"Transformer" and "LLM" are exact synonyms.
Show answer
False. Not all transformers are LLMs (some do vision), and not all LLMs are transformers.
Finetuning generally requires less data and compute than pretraining.
Show answer
True. It builds on capabilities the base model already has.
GPT models can only do the exact task they were trained on.
Show answer
False. They show emergent behavior — e.g. translation and summarization — not explicitly taught.
Part C · Short answer
Name the three stages the book uses to build an LLM.
Show answer
Stage 1: building the LLM (data prep, attention, architecture). Stage 2: pretraining to create a foundation model (plus loading pretrained weights). Stage 3: finetuning it into a classifier or an instruction-following assistant.