Before we write a single line of PyTorch, it pays to have the whole journey on one map: what a large language model large language model A deep neural network trained on massive amounts of text to understand and generate human-like language; "large" refers to both its billions of parameters and its huge training corpus. defined in ch. 1 — open in glossary (LLM) actually is, why the old way of building language software gave way to it, and the exact two-step recipe — pretrain, then fine-tune — that the rest of this book implements from scratch.
1.1 What is an LLM?
An LLM is a deep neural network trained on enormous amounts of text to understand and generate human-like language. The word large is doing double duty: it refers both to the model’s size — tens to hundreds of billions of parameters parameters The learnable weights of a neural network. GPT-3 has 175 billion of them. defined in ch. 1 — open in glossary (GPT-3 has 175 billion) — and to the data it was trained on: hundreds of billions of words.
LLMs sit inside a nest of older, broader ideas. Artificial intelligence contains machine learning; machine learning contains deep learning deep learning Machine learning with multi-layer neural networks that learn their own representations from raw data, without manual feature engineering. defined in ch. 1 — open in glossary ; and LLMs are a particular, very large kind of deep-learning model. They are also a form of generative AI generative ai AI that creates new content such as text, images, or code. LLMs are the text-generation instance of generative AI. defined in ch. 1 — open in glossary — software that produces new content (here, text) rather than only classifying existing content.
Why deep learning changed everything
The break with classical natural-language software is that LLMs need no manual feature engineering. In traditional machine learning, a human expert had to decide, up front, which measurable properties of the input the model should look at. For a spam filter that meant hand-picking features: the frequency of trigger words like “prize”, “win”, “free”; the count of exclamation marks; whether the text is ALL CAPS; the presence of suspicious links. Deep learning throws that step away — the network learns its own features directly from raw text.
1.2 Applications of LLMs
Because a single pretrained model captures so much about language, one LLM can be pointed at a remarkable range of tasks: drafting and completing text, machine translation, sentiment analysis, summarization, question answering, and powering chatbots and virtual assistants. The same core network that autocompletes a sentence can, with the right prompt or a little fine-tuning, answer a support ticket or classify an email. This versatility is why LLMs have become a general-purpose tool rather than one-task software.
1.3 The two stages: pretraining and fine-tuning
Building and using an LLM happens in two distinct training stages — the backbone of this entire book.
- Pretraining pretraining The first training stage: self-supervised next-word prediction on large unlabeled text, producing a general-purpose foundation model. defined in ch. 1 — open in glossary . The model is trained on a huge pile of ordinary, unlabeled text (web pages, books, Wikipedia) by playing one endless game: predict the next word. Because the “answer” — the next word — is already sitting in the text, no human has to label anything; this is self-supervised learning self-supervised learning Training where the labels are derived from the data itself (e.g. predicting the next word), requiring no human annotation. defined in ch. 1 — open in glossary . The result is a broadly capable foundation model foundation model The general-purpose pretrained LLM, before any task-specific fine-tuning. defined in ch. 1 — open in glossary with text-completion and few-shot few-shot Solving a task given a handful of examples in the prompt. defined in ch. 1 — open in glossary abilities.
- Fine-tuning fine-tuning A later training stage on a smaller labeled dataset that specializes the pretrained model to a specific task. defined in ch. 1 — open in glossary . The foundation model is then trained a little more on a smaller, labeled dataset to specialize it. The two most common flavors — both built later in this book — are instruction fine-tuning instruction fine-tuning Fine-tuning on (instruction, response) pairs so the model learns to follow natural-language instructions (see ch7). defined in ch. 1 — open in glossary (on instruction–response pairs, Chapter 7) and classification fine-tuning classification fine-tuning Fine-tuning on (text, class-label) pairs for a categorization task such as spam detection (see ch6). defined in ch. 1 — open in glossary (on text–label pairs, Chapter 6).
1.4 Using LLMs for different tasks — the transformer
Almost every modern LLM is built on the transformer transformer The neural architecture behind modern LLMs — an encoder and/or decoder built around attention — introduced in "Attention Is All You Need" (2017). defined in ch. 1 — open in glossary , the architecture introduced in the 2017 paper “Attention Is All You Need.” The original transformer was designed for machine translation and has two halves:
- an encoder encoder The transformer half that maps an input sequence into context-carrying embedding vectors. BERT is encoder-only. defined in ch. 1 — open in glossary that reads the entire input sentence and compresses it into context-carrying embedding vectors, and
- a decoder decoder The transformer half that generates output tokens one at a time. GPT is decoder-only; this book builds a decoder-only model. defined in ch. 1 — open in glossary that generates the translated sentence one word at a time, consulting those vectors.
Walk through how the original transformer translates “This is an example” into German, one numbered stage at a time:
Input text to be translated
The source sentence "This is an example" enters the encoder side.
The original encoder–decoder transformer translating one sentence (book Figure, p. 5).
Key idea
The single most important part of a transformer is the self-attention self-attention A mechanism that lets each token weigh the relevance of every other token in the sequence. It is the core component of the transformer. defined in ch. 1 — open in glossary mechanism: it lets the model weigh how relevant every token is to every other token in the sequence. Self-attention is what Chapter 3 builds from scratch, and it is the reason transformers handle long-range context so well.BERT vs. GPT: two children of the transformer
You do not always need both halves. Two hugely influential model families each kept only one:
- BERT bert An encoder-only transformer trained by masked-word prediction; strong at classification and language understanding. defined in ch. 1 — open in glossary (Bidirectional Encoder Representations from Transformers) uses the encoder and is trained by masked-word prediction: random words are hidden and the model fills them back in. It sees text in both directions, which makes it strong at classification and understanding.
- GPT gpt A decoder-only transformer trained by next-word (autoregressive) prediction; strong at text generation. The model built in this book. defined in ch. 1 — open in glossary (Generative Pretrained Transformer) uses the decoder and is trained by next-word prediction, generating text one token at a time — it is autoregressive autoregressive Generating each token conditioned on all previously generated tokens — how GPT produces text. defined in ch. 1 — open in glossary . This is the design we build in this book.
Interestingly, GPT models — trained only to predict the next word — turn out to be capable at tasks they were never explicitly taught, from translation to summarization. That surprising generality, which grows with scale, is the “emergent behavior” we look at next in §1.5–1.8.
📝 Check yourself: what an LLM is
0 / 41.What is the single biggest practical difference between deep learning and traditional machine learning for a task like spam detection?
2.In the two-stage recipe, what exactly is a 'foundation model'?
3.The book builds a GPT-style model. How does GPT differ architecturally from BERT?
4.Pretraining an LLM is described as 'self-supervised'. Where do the training labels come from?