Chapter 1: How AI works
The chapter introduces its scope and explains how AI and machine learning relate.
After this chapter you will be able to
- AI and machine learning relationship. Distinguish AI from machine learning and explain why the terms are sometimes used interchangeably in this book.
- Next Word Prediction. Describe the task current LLMs are designed to perform and explain why that task can involve other abilities.
- Self Contained Static Model. Explain how the section says LLMs are created and what being self-contained and static means for their outputs.
- Wrapper And Underlying LLM. Distinguish the underlying LLM from the wrapper people typically interact with, using the section’s examples of added functionality.
- Repeated word generation. Trace how an LLM wrapper turns single-word predictions into a sentence by appending each predicted word to the prompt and querying the LLM again.
- End-of-text stopping signal. Explain how an LLM comes to output an end-of-text code word and what its wrapper does when it encounters that word.
- Chat versus completion behavior. Explain why ChatGPT treated “The Eiffel” as an incomplete question while completions mode continued it with “ Tower.” Describe the role of the chat wrapper’s end-of-prompt marker.
- System prompt as added context. Explain why ChatGPT could answer a question about the current date even though the LLM does not have access to real-time data.
- External function call flow. Trace how an LLM suggests a function call, the wrapper runs it, and the result is added to the prompt so the LLM can use it.
- RAG retrieval and augmentation. Trace how a RAG-based LLM wrapper searches for relevant documents when a user submits a prompt, inserts their content into the prompt, and lets the LLM use that content when generating text.
- Tokens and text composition. Explain that a token is an element of an LLM’s fixed-size vocabulary and may be a word, a piece of a word, a character, a symbol, or a special code word. Describe how combining tokens lets an LLM handle words outside its vocabulary and non-Latin text.
- Prompt tokenization and IDs. Explain how the LLM wrapper prepares a text prompt by splitting it into vocabulary tokens and converting them to token IDs before passing them to the LLM.
- Next-token generation and stopping. Trace how the wrapper generates text by requesting a next-token prediction, attaching that token to the prompt, and repeating until an end-of-text token tells it to stop.
- Input and output token costs. Explain that most LLM APIs charge for both input and output tokens, so longer prompts and responses cost more and repeated calls can make bills add up.
- Growing chat history costs. Explain why a back-and-forth chat can become more expensive over time when the LLM needs to consider earlier conversation.
- English-biased tokenization. Explain why text in languages other than English can require more tokens, using the French and Telugu examples from the section.
- Costs of extra tokens. Describe the two consequences the section gives for using more tokens to represent non-English text.
- Meaningful units for text. Explain why tokens such as “Paris” are more straightforward for an LLM to read and generate than individual characters.
- Practical token vocabulary balance. Explain why a token vocabulary of common words and word pieces is preferred over one containing every word derivative and phrase.
- Embedding vectors and dimensions. Explain that an embedding represents a token’s meaning as a vector of numbers rather than as its letters, and that its dimension is the number of elements in that vector.
- Embeddings in token prediction. Explain why LLMs seek good contextualized embeddings of input tokens and how longer vectors allow them to represent many more topics than the five-number illustration.
- Machine-learned embedding dimensions. Explain why engineers use machine learning rather than design long embeddings by hand, and how the machine determines what their dimensions represent.
- Embedding space and proximity. Explain how numbers in an embedding vector act like coordinates in a meaning space, and how the distance between tokens reflects how closely related they are in meaning.
- Comparing meanings with dot products. Explain how the section uses a dot product to compare two embedding vectors and interprets positive, zero, and negative results.
- Projection into fewer dimensions. Describe how a projection extracts selected information from an embedding by reducing its dimensions, and state how the projection is performed and determined.
- Letter analysis limitation. Explain why an LLM may struggle to count letters in a word or follow instructions about particular letters: it receives tokens rather than individual letters, and token embeddings represent meaning rather than reliably preserving spelling details.
- Context Without Compression. Explain why compressing earlier tokens into one embedding can make an LSTM lose information, and how a transformer instead uses previous tokens to disambiguate a token such as “bark.”
- Three Transformer Steps. Trace the section’s three-step account from separate initial token embeddings, through contextualized embeddings, to predicting the next token.
- Context Window Limits. Describe what the context window limits and explain why prompt length matters for long documents, retrieved material, and chat history.
- Context-free embedding lookup. Describe how the LLM creates initial embeddings by looking up each token in an internal dictionary and replacing it with the corresponding embedding, one token at a time without context.
- Training of dictionary values. Explain that the numbers in the embedding dictionary are learnable parameters, not values chosen by hand, and that training fills them in to form an embedding space useful for guessing the next word.
- Contextualization through attention. Explain how attention scores indicate which tokens in the context window are most relevant when disambiguating a token, using “dog’s bark” as an example.
- Contextualized embedding updates. Describe how attention scores guide the updating of token embeddings and why the updated embeddings help with predicting the next token.
- Next-token probability output. Explain that the prediction step projects contextualized embeddings into a probability for every token in the vocabulary, rather than directly outputting only the most likely token.
- Wrapper token selection. Describe how the LLM wrapper chooses a next token from the LLM’s output probabilities, including the option of choosing the highest-probability token.
- Temperature and sampling probabilities. Explain how low and high temperature change sampling probabilities and the resulting tendency to select obvious or alternative tokens.
- Theoretical top-one token selection. Describe how top-1 sampling could, in theory, contribute to reproducible output.
- Practical limits of reproducibility. Explain why identical output cannot always be guaranteed in practice, even when settings aim for reproducibility.
- Mathematical operations in LLM code. Explain what a quick look at GPT-2’s code reveals about how an LLM is implemented, including the operations the section identifies within its layers.
- Architecture and model parameters. Explain how an engineer designs a machine learning model’s architecture while leaving its parameters to be set during training. Use the embedding-and-projection example to illustrate the distinction.
- Data-guided parameter training. Describe what training does to a model’s parameters and how data guides that process.
- Learned versus engineered features. Explain how earlier machine learning uses features extracted by an engineer, while deep learning learns a useful representation from rawer input within the model.
- Deep and shallow tradeoffs. Distinguish the situations in which the section favors deep learning from those in which a shallow, more explainable model may suffice.
- Labeled Examples and Generalization. Explain how supervised learning uses input–label pairs, and distinguish generalization from overfitting and underfitting.
- Automatically Extracted Training Labels. Describe how self-supervised learning creates LLM training examples from existing text and why it remains supervised learning.
- Trial and Error Learning. Describe reinforcement learning as learning from feedback on tried actions, including the exploration–exploitation trade-off.
- Unsupervised Patterns and Task Fit. Distinguish unsupervised pattern-finding from predicting a known outcome, and explain why cluster quality depends on the intended use.
- Next-token objective mismatch. Explain why success at predicting internet text did not guarantee factual, appropriate answers or obedience to user instructions.
- Human-guided training stages. Trace the four InstructGPT training steps: self-supervised training, human-written prompt-and-answer examples, fine-tuning on those examples, and reinforcement learning from human rankings of alternative outputs.
- Purpose of model loss. Explain how a learning algorithm uses loss to compare models with different parameter values, and how the loss for a dataset is obtained from its training examples.
- Next-token loss calculation. Describe how the probability assigned to the correct next token determines an LLM training example’s cross-entropy loss.
- Limits of training loss. Explain why a low loss on training data does not necessarily mean a model will perform well on new data.
- Stochastic gradient descent process. Trace how SGD starts with random parameters, uses a minibatch to estimate a gradient, and makes a small parameter update intended to reduce loss. Explain that it repeats across batches and usually makes several full passes, or epochs, over the training data.
- CNN image classification pipeline. Trace how a CNN turns an image of pixels into a category prediction through repeated convolutions and downsampling, an abstract embedding, and probabilities for possible categories.
- Learned filters and patterns. Explain what a convolutional filter can detect, how its values are chosen, and why repeated filtering can reveal increasingly complex patterns.
- Downsampling and object location. Justify why a CNN downsamples images when categorizing objects, relating this to its assumption about object parts and their precise locations.
- Multimodal image-to-text connection. Explain what makes an AI model multimodal and how one image-to-text approach connects a CNN to an LLM.
- Caption-guided diffusion training. Describe how the conditional diffusion model in this section is trained to repair images using captions.
- Iterative image generation. Trace how the trained diffusion model generates a new image from a text description.
- Task-specific model architectures. Explain why machine learning uses architectures tailored to particular problems and what the No Free Lunch Theorem says about a universally best architecture.
Section by section
Introduction
The chapter introduces its scope and explains how AI and machine learning relate.
- AI and machine learning relationship: Distinguish AI from machine learning and explain why the terms are sometimes used interchangeably in this book.
Why it matters: This distinction helps you follow when the book is discussing AI broadly and when it means today's machine-learning-based methods.
Key terms: AI, machine learning
1.1 How LLMs work
This section explains an LLM’s next-word prediction task, why that task supports varied abilities, and how the underlying model differs from the software people use to interact with it.
- Language Model Size: Explain what a language model tries to represent and what makes an LLM different.
Why it matters: This establishes what the underlying model is before considering what it does. - Next Word Prediction: Describe the task current LLMs are designed to perform and explain why that task can involve other abilities.
Why it matters: It explains how one apparently simple prediction task can underlie varied outputs. - Self Contained Static Model: Explain how the section says LLMs are created and what being self-contained and static means for their outputs.
Why it matters: It separates information in the underlying model from information supplied by other software. - Wrapper And Underlying LLM: Distinguish the underlying LLM from the wrapper people typically interact with, using the section’s examples of added functionality.
Why it matters: It prevents capabilities of the customer-facing application from being attributed to the bare model.
Key terms: language model, large language model, LLM, prompt, autocomplete, autoregressive, machine learning, self-contained, static, LLM wrapper
1.2 Text generation
This section explains how an LLM wrapper generates sentences by repeatedly adding each predicted word to the prompt and asking for the next one.
- Repeated word generation: Trace how an LLM wrapper turns single-word predictions into a sentence by appending each predicted word to the prompt and querying the LLM again.
Why it matters: This explains how repeated single-word predictions can produce a full sentence.
Key terms: LLM wrapper, prompt
1.3 End of text
This section explains how an LLM signals that its text is finished and how its wrapper stops generation.
- End-of-text stopping signal: Explain how an LLM comes to output an end-of-text code word and what its wrapper does when it encounters that word.
Why it matters: It explains how text generation ends rather than continuing indefinitely.
Key terms: special code word, <|end of text|>, LLM wrapper
1.4 Chat
The section explains how a chat wrapper uses boundary markers to make an LLM respond conversationally rather than simply continue the user's text.
- Chat versus completion behavior: Explain why ChatGPT treated “The Eiffel” as an incomplete question while completions mode continued it with “ Tower.” Describe the role of the chat wrapper’s end-of-prompt marker.
Why it matters: It explains why the same words can produce a conversational reply in chat mode and a text continuation in completions mode. - Conversation history boundaries: Describe what the chat wrapper sends as a conversation continues and why it includes boundary markers.
Why it matters: Those boundaries help the LLM distinguish the interactions in an ongoing chat.
Key terms: LLM wrapper, <|end of prompt|>, completions mode, chat mode, history of the entire conversation, <|end of text|>
1.5 The system prompt
This section explains how a system prompt supplies an LLM with context, using current-date answers to show the difference that context makes.
- System prompt as added context: Explain why ChatGPT could answer a question about the current date even though the LLM does not have access to real-time data.
Why it matters: A correct-looking answer about current information does not mean the LLM obtained that information on its own. - Developer-controlled system prompts: Describe how the API example shows the effect of providing or omitting a system prompt containing the date.
Why it matters: The API comparison makes the system prompt’s effect visible by changing whether the date is supplied.
Key terms: system prompt, current date, API, GPT-4o
1.6 Calling external software functions
This section explains how a chatbot can appear to use real-time information by having an LLM suggest a function call and a wrapper supply the function’s result.
- External function call flow: Trace how an LLM suggests a function call, the wrapper runs it, and the result is added to the prompt so the LLM can use it.
Why it matters: It explains why a chatbot can answer a live-weather question without the LLM itself having live access. - Function descriptions and implementation: Distinguish the function description supplied to the LLM from the function code a developer must write and run.
Why it matters: It prevents confusing a function the LLM can suggest with a working source of live data.
Key terms: LLM wrapper, software functions, prompt, function declaration, function inputs, system prompt, get_current_weather
1.7 Retrieval-augmented generation
This section explains how retrieval-augmented generation gives an LLM relevant documents through its prompt, what it is used for, and its main limitations.
- RAG retrieval and augmentation: Trace how a RAG-based LLM wrapper searches for relevant documents when a user submits a prompt, inserts their content into the prompt, and lets the LLM use that content when generating text.
Why it matters: This is the basic sequence behind using documents the LLM was not trained on. - Uses of retrieved documents: Explain why RAG is useful for documents absent from training data, including internal business documents or up-to-date webpages, and how it can help identify sources for citations.
Why it matters: It explains why a business might retrieve documents rather than rely on the LLM's training data alone. - Retrieval and prompt constraints: Describe the challenges of finding relevant documents and fitting their content within the LLM's maximum prompt length, including the cost of longer prompts.
Why it matters: Finding and adding documents is useful only if the material is relevant and the resulting prompt is practical to send.
Key terms: retrieval-augmented generation, RAG, LLM wrapper, retrieval, augmented, training data, in-house chatbot, up-to-date web content, cite references, relevant documents
1.8 The concept of tokens
The section replaces the idea of LLMs predicting whole words with an explanation of tokens, what they can represent, and how an LLM’s vocabulary is assembled.
- Tokens and text composition: Explain that a token is an element of an LLM’s fixed-size vocabulary and may be a word, a piece of a word, a character, a symbol, or a special code word. Describe how combining tokens lets an LLM handle words outside its vocabulary and non-Latin text.
Why it matters: It makes “guessing the next word” a more accurate description of what an LLM does. - Leading spaces in tokens: Describe how a common word can have a token with a leading space as well as one without it, so the LLM need not always use a separate whitespace token.
Why it matters: It shows why token boundaries do not always match word boundaries. - Vocabulary pattern selection: Describe how an LLM vocabulary is made by automatically analyzing many documents for common text patterns, and state its approximate size in current LLMs.
Why it matters: It explains why common text patterns become tokens in a vocabulary of limited size.
Key terms: token, vocabulary, leading space, whitespace token, text patterns, byte pair encoding
1.9 One token at a time
This section explains how an LLM wrapper turns a prompt into token IDs and generates text one token at a time until an end-of-text token stops it.
- Prompt tokenization and IDs: Explain how the LLM wrapper prepares a text prompt by splitting it into vocabulary tokens and converting them to token IDs before passing them to the LLM.
Why it matters: It clarifies what the model actually receives when you enter a prompt. - Next-token generation and stopping: Trace how the wrapper generates text by requesting a next-token prediction, attaching that token to the prompt, and repeating until an end-of-text token tells it to stop.
Why it matters: It explains both how a longer response grows and how it can end. - Words assembled from tokens: Describe how a word absent from the model’s vocabulary can still appear in its output as a sequence of tokens, using “hungryish” as the example.
Why it matters: It shows why a token should not be assumed to mean a whole word.
Key terms: LLM wrapper, tokens, vocabulary, token ID, next token, eat its own output, <|end of text|>, hungryish, hungry, ish
1.10 Billed by the token
Explains how token-based LLM API pricing makes long prompts, long responses, repeated calls, and growing chat histories more expensive.
- Input and output token costs: Explain that most LLM APIs charge for both input and output tokens, so longer prompts and responses cost more and repeated calls can make bills add up.
Why it matters: It helps you estimate the cost of using an LLM in an application, not just the cost of one call. - Growing chat history costs: Explain why a back-and-forth chat can become more expensive over time when the LLM needs to consider earlier conversation.
Why it matters: It explains why later turns in a long conversation may cost more than earlier ones.
Key terms: LLM APIs, input tokens, output tokens, chat history, prompt
1.11 What about languages other than English?
The section explains why English-oriented token vocabularies use more tokens for many other languages, what that costs, and why improving tokenization involves tradeoffs.
- English-biased tokenization: Explain why text in languages other than English can require more tokens, using the French and Telugu examples from the section.
Why it matters: It explains why the same content can be represented very differently depending on its language. - Costs of extra tokens: Describe the two consequences the section gives for using more tokens to represent non-English text.
Why it matters: Tokenization differences affect both what users pay and how much context the model must assemble to interpret text. - Vocabulary expansion tradeoff: Explain why adding tokens for other languages is not a simple fix, and describe the improvement example given for GPT-4o.
Why it matters: It shows both the constraint on a straightforward fix and that improvements need not eliminate the disparity.
Key terms: vocabulary, tokens, UTF-8 tokens, billed by the token, contextualize adjacent tokens, vocabulary size, GPT-4o, Telugu
1.12 Why do LLMs need tokens anyway?
The section explains why LLMs use tokens rather than individual characters or an enormous vocabulary of whole phrases.
- Meaningful units for text: Explain why tokens such as “Paris” are more straightforward for an LLM to read and generate than individual characters.
Why it matters: It explains why the model works with chunks of text rather than always handling one character at a time. - Practical token vocabulary balance: Explain why a token vocabulary of common words and word pieces is preferred over one containing every word derivative and phrase.
Why it matters: It shows why making tokens larger is useful only up to a point.
Key terms: tokens, characters, meaning, context, vocabulary, words, pieces of words
1.13 Embeddings: A way to represent meaning
This section introduces embeddings as numerical representations of token meaning and explains why they matter to an LLM’s next-token prediction.
- Embedding vectors and dimensions: Explain that an embedding represents a token’s meaning as a vector of numbers rather than as its letters, and that its dimension is the number of elements in that vector.
Why it matters: This gives you the basic vocabulary for how the section describes token meaning. - Illustrative topic scores: Describe how the section’s imaginary five-number embeddings use each position as a score for a topic, and use the dog and elephant examples to interpret positive, negative, and neutral values.
Why it matters: The simplified examples make an otherwise abstract numerical representation easier to picture. - Embeddings in token prediction: Explain why LLMs seek good contextualized embeddings of input tokens and how longer vectors allow them to represent many more topics than the five-number illustration.
Why it matters: It connects the numerical representation to the LLM’s task of predicting what comes next.
Key terms: embedding, vector, dimension, token, embedding vector, topic, neutral value, LLMs, contextualized representation, embeddings
1.14 Machine learning and embeddings
This section explains why machine learning creates LLM embeddings and why that makes their dimensions difficult to interpret.
- Machine-learned embedding dimensions: Explain why engineers use machine learning rather than design long embeddings by hand, and how the machine determines what their dimensions represent.
Why it matters: This distinguishes choosing an embedding’s size from manually assigning meanings to its dimensions. - Predictive power and explainability: Explain why automatically created embeddings are difficult to interpret despite being effective.
Why it matters: An embedding can be useful even when its individual dimensions have no clear explanation.
Key terms: machine learning, embeddings, dimensions, topics, explainability, predictive power
1.15 Visualizing embeddings
This section uses a visual analogy to show how embedding vectors place tokens in a meaning space where related tokens cluster.
- Embedding space and proximity: Explain how numbers in an embedding vector act like coordinates in a meaning space, and how the distance between tokens reflects how closely related they are in meaning.
Why it matters: This picture makes it easier to understand what an embedding represents and what makes one useful.
Key terms: embedding vector, token, space of meaning, high-dimensional
1.16 Why embeddings are useful
This section explains how simple calculations on embeddings can compare meanings, extract selected information, and match content across text and images.
- Comparing meanings with dot products: Explain how the section uses a dot product to compare two embedding vectors and interprets positive, zero, and negative results.
Why it matters: It shows how embeddings make meaning comparisons possible with a simple calculation. - Projection into fewer dimensions: Describe how a projection extracts selected information from an embedding by reducing its dimensions, and state how the projection is performed and determined.
Why it matters: Projection explains how useful information can be drawn from an embedding without keeping all of its dimensions. - Embedding-based content matching: Explain how generated embeddings can be used to retrieve related documents or match a text description to an image.
Why it matters: It connects the vector calculations to practical searches across documents and images.
Key terms: embedding vectors, dot product, embedding space, projection, matrix, lower-dimensional space, machine learning, embeddings API, content-retrieval applications, input modalities
1.17 Why LLMs struggle to analyze individual letters
The section explains why token-based inputs and meaning-focused embeddings make individual letters difficult for LLMs to analyze, and notes a possible workaround.
- Letter analysis limitation: Explain why an LLM may struggle to count letters in a word or follow instructions about particular letters: it receives tokens rather than individual letters, and token embeddings represent meaning rather than reliably preserving spelling details.
Why it matters: It connects a familiar LLM failure to how text is represented before the model works with it. - Possible spelling workaround: Describe the section’s proposed ad hoc response to letter-level questions and its limitation: a wrapper might add word spellings to the prompt, but the problem has not been fully solved.
Why it matters: It distinguishes a possible way to supply missing spelling information from a solved limitation.
Key terms: tokens, embedding vector, individual letters, LLM wrapper, prompt, words’ spellings
1.18 The transformer architecture
This section explains how transformers use token context to predict the next token, why that improves on LSTMs, and why context-window limits matter.
- Context Without Compression: Explain why compressing earlier tokens into one embedding can make an LSTM lose information, and how a transformer instead uses previous tokens to disambiguate a token such as “bark.”
Why it matters: It explains why preserving token-level context helps a model choose a fitting continuation. - Three Transformer Steps: Trace the section’s three-step account from separate initial token embeddings, through contextualized embeddings, to predicting the next token.
Why it matters: This sequence shows where context enters before the model chooses a continuation. - Context Window Limits: Describe what the context window limits and explain why prompt length matters for long documents, retrieved material, and chat history.
Why it matters: The window determines whether the material you want the model to consider fits in its prompt.
Key terms: LSTM, embedding, transformer architecture, context, initial embeddings, contextualization, contextualized embeddings, next token, context window, tokens
1.19 Step 1: Initial embeddings
This section explains how an LLM creates initial token embeddings by lookup, how training determines their values, and why the lookup dictionary can contain many learnable parameters.
- Context-free embedding lookup: Describe how the LLM creates initial embeddings by looking up each token in an internal dictionary and replacing it with the corresponding embedding, one token at a time without context.
Why it matters: It distinguishes the initial lookup from the contextualization that follows. - Training of dictionary values: Explain that the numbers in the embedding dictionary are learnable parameters, not values chosen by hand, and that training fills them in to form an embedding space useful for guessing the next word.
Why it matters: It explains where the values used in the initial lookup come from. - Embedding dictionary parameter count: Apply the section’s example to state that a vocabulary of 100,000 tokens with 10,000 numbers per embedding yields one billion learnable dictionary parameters.
Why it matters: The calculation shows how large the initial embedding dictionary alone can be.
Key terms: initial embeddings, token, dictionary, without context, learnable parameters, learning, training, embedding space, vocabulary, dimensions
1.20 Step 2: Contextualization
This section explains how a transformer uses attention to update token embeddings in context, and how layers and attention heads extend that process.
- Contextualization through attention: Explain how attention scores indicate which tokens in the context window are most relevant when disambiguating a token, using “dog’s bark” as an example.
Why it matters: Attention helps explain how the same token can take on the meaning suggested by its context. - Contextualized embedding updates: Describe how attention scores guide the updating of token embeddings and why the updated embeddings help with predicting the next token.
Why it matters: The updated embeddings are how useful context becomes part of the representation used for the next prediction. - Learned attention parameters: Explain the division of work between the engineer and machine learning in the attention calculations, at the level described in this section.
Why it matters: It distinguishes the designed calculation from the values the model learns. - Layered and multiheaded contextualization: Distinguish the two extensions described here: repeating contextualization across transformer layers and using attention heads to analyze embedding segments separately.
Why it matters: Layers and heads describe two different ways the transformer extends the basic contextualization step.
Key terms: contextualization, attention score, attention mechanism, context window, embedding vectors, feed-forward step, contextualized representation, projections, projection matrixes, learnable parameters
1.21 Step 3: Predictions
This section explains that an LLM produces probabilities for every possible next token, then a wrapper uses those probabilities to choose a token.
- Next-token probability output: Explain that the prediction step projects contextualized embeddings into a probability for every token in the vocabulary, rather than directly outputting only the most likely token.
Why it matters: It distinguishes what the LLM calculates from the token that eventually appears. - Wrapper token selection: Describe how the LLM wrapper chooses a next token from the LLM’s output probabilities, including the option of choosing the highest-probability token.
Why it matters: It explains why a probability distribution does not by itself determine which token is selected.
Key terms: contextualized embeddings, vocabulary, probability value, vector, LLM wrapper, output probabilities, highest probability
1.22 Temperature
This section explains how sampling settings affect which token an LLM wrapper selects next and how conservative or adventurous the output may be.
- Probability-based token selection: Explain why a sampler may choose the next token using the LLM’s probabilities rather than always choosing the highest-probability token.
Why it matters: This is the basis for understanding what the section’s sampling settings change. - Temperature and sampling probabilities: Explain how low and high temperature change sampling probabilities and the resulting tendency to select obvious or alternative tokens.
Why it matters: Temperature is the section’s main way to regulate how conservative or adventurous token selection is. - Top-p and Top-k limits: Distinguish how Top-p and Top-k restrict the tokens available for sampling.
Why it matters: These settings offer two other ways to limit how adventurous token selection can be.
Key terms: token, vocabulary, output probabilities, sampler, temperature, sampling probabilities, LLM wrapper, Top-p, cumulative probability, Top-k
1.23 Can you get an LLM to always output the same thing?
The section defines reproducible LLM output, explains a theoretical way to achieve it, and shows why current APIs may still produce different results.
- Reproducible output and its uses: Explain what reproducible LLM output means and why it can be useful.
Why it matters: It clarifies what someone is asking for when they want repeatable LLM results. - Theoretical top-one token selection: Describe how top-1 sampling could, in theory, contribute to reproducible output.
Why it matters: It shows why eliminating randomness from token choice looks like a route to identical outputs. - Practical limits of reproducibility: Explain why identical output cannot always be guaranteed in practice, even when settings aim for reproducibility.
Why it matters: It prevents treating a mostly repeatable API response as a guarantee of exact equality.
Key terms: reproducible outputs, input prompt, benchmark, top-1 sampling, token, highest probability, mostly reproducible outputs, threads, round-off errors
1.24 Where to learn more
The section points readers to resources for exploring LLM implementation details and notes what a skim of the code reveals about its operations.
- Mathematical operations in LLM code: Explain what a quick look at GPT-2’s code reveals about how an LLM is implemented, including the operations the section identifies within its layers.
Why it matters: It connects the earlier account of LLM predictions to what readers can see in an actual implementation. - Sources for implementation details: Describe the resources the author recommends for learning implementation and architecture details, and the suggested way to approach the code.
Why it matters: These resources give readers a path from the chapter’s overview to implementation and architecture details.
Key terms: block, attention scores, embeddings, projections, GPT-2, models.py, Python, TensorFlow, The Illustrated Transformer
1.25 Machine learning
This section explains machine learning as designing a model’s architecture and then using data-driven training to set its parameters, rather than specifying the entire program by hand.
- Architecture and model parameters: Explain how an engineer designs a machine learning model’s architecture while leaving its parameters to be set during training. Use the embedding-and-projection example to illustrate the distinction.
Why it matters: It separates what engineers design from what the model learns. - Data-guided parameter training: Describe what training does to a model’s parameters and how data guides that process.
Why it matters: It explains what the learning step contributes after the architecture has been designed. - Advantages of data-driven models: Explain why a trained model can outperform a program written entirely by hand, despite the cost of training.
Why it matters: It explains why the expense of training may be worthwhile.
Key terms: machine learning, model, architecture, building blocks, parameters, embedding, training, learning, data, data-driven
1.26 Deep learning
This section contrasts deep learning with earlier feature-engineered machine learning and explains when each approach may be useful.
- Learned versus engineered features: Explain how earlier machine learning uses features extracted by an engineer, while deep learning learns a useful representation from rawer input within the model.
Why it matters: The distinction explains what changes when a system uses deep learning rather than manually engineered features. - Depth and input representations: Describe why the term “deep” is used and how the section illustrates learned representations with LLMs.
Why it matters: The LLM example makes learned representations concrete, while the stacked layers explain the name. - Deep and shallow tradeoffs: Distinguish the situations in which the section favors deep learning from those in which a shallow, more explainable model may suffice.
Why it matters: Model choice depends on the input and the need to explain outputs, not simply on whether deep learning is available.
Key terms: deep learning, feature engineering, raw input, manually engineered features, layers, contextualized embeddings, input tokens, shallow learning, effective architecture, explainable model
1.27 Types of machine learning
The section distinguishes four machine-learning paradigms by how they obtain training information and explains how LLMs use self-supervised examples and reinforcement learning with human feedback.
- Labeled Examples and Generalization: Explain how supervised learning uses input–label pairs, and distinguish generalization from overfitting and underfitting.
Why it matters: This gives you the baseline for comparing the other learning paradigms. - Limits of Synthetic Data: Explain why simulated or augmented examples are not generally a substitute for obtaining correctly labeled data.
Why it matters: More generated examples do not automatically solve the problem of obtaining reliable answers. - Automatically Extracted Training Labels: Describe how self-supervised learning creates LLM training examples from existing text and why it remains supervised learning.
Why it matters: It explains how LLMs can obtain vast numbers of training examples without manually labeling each one. - Trial and Error Learning: Describe reinforcement learning as learning from feedback on tried actions, including the exploration–exploitation trade-off.
Why it matters: It distinguishes learning from feedback on actions from learning directly from supplied correct answers. - Human Feedback for LLMs: Trace how reinforcement learning with human feedback uses human preferences to refine an existing LLM.
Why it matters: It shows how feedback about preferred outputs can improve an LLM that already exists. - Unsupervised Patterns and Task Fit: Distinguish unsupervised pattern-finding from predicting a known outcome, and explain why cluster quality depends on the intended use.
Why it matters: It helps you avoid using an appealing grouping method when the real task is to predict a labeled outcome.
Key terms: supervised learning, input, label, training data, generalize, overfitting, simulated training samples, synthetic data, SMOTE, labels
1.28 How LLMs are trained (and tamed)
The section explains why next-token training alone did not produce reliably helpful and safe LLMs, how human feedback was added to training, and what that means for user privacy.
- Self-supervised training ambition: Explain how early LLMs were trained on large collections of internet text using automatically generated examples, and why researchers thought next-token prediction might teach them many other tasks.
Why it matters: This explains why internet text initially seemed sufficient for training powerful LLMs. - Next-token objective mismatch: Explain why success at predicting internet text did not guarantee factual, appropriate answers or obedience to user instructions.
Why it matters: It identifies the problem that the later human-guided training steps aim to address. - Human-guided training stages: Trace the four InstructGPT training steps: self-supervised training, human-written prompt-and-answer examples, fine-tuning on those examples, and reinforcement learning from human rankings of alternative outputs.
Why it matters: It shows why producing a more useful LLM involves substantial human work, not just collecting internet text. - Conversation data privacy: Describe how providers may use recorded conversations and user ratings to improve LLMs, and why users should be cautious about putting sensitive information in prompts.
Why it matters: A prompt may not remain private merely because it feels like a one-to-one conversation.
Key terms: self-supervised learning, next-token-prediction task, misalignment, language modeling objective, next token, InstructGPT, self-supervised, fine-tuned, reinforcement learning with human feedback, prompts
1.29 Loss
This section explains how loss measures a model’s errors on training examples, how an LLM’s next-token loss is calculated, and why low training loss does not guarantee good performance on new data.
- Purpose of model loss: Explain how a learning algorithm uses loss to compare models with different parameter values, and how the loss for a dataset is obtained from its training examples.
Why it matters: Loss gives the learning algorithm a way to tell whether one model fits its training examples better than another. - Next-token loss calculation: Describe how the probability assigned to the correct next token determines an LLM training example’s cross-entropy loss.
Why it matters: This makes precise what it means for an LLM to do well or poorly on a correct next-token answer. - Limits of training loss: Explain why a low loss on training data does not necessarily mean a model will perform well on new data.
Why it matters: A good training score alone is not proof that a model will work well beyond its training examples.
Key terms: loss, cost, training examples, parameter values, correct next token, probability, cross-entropy loss, log loss, negative logarithm, training data
1.30 Stochastic gradient descent
This section explains how stochastic gradient descent trains a model, why its results have limits, and how training differs from inference, then clarifies two meanings of “generative AI.”
- Stochastic gradient descent process: Trace how SGD starts with random parameters, uses a minibatch to estimate a gradient, and makes a small parameter update intended to reduce loss. Explain that it repeats across batches and usually makes several full passes, or epochs, over the training data.
Why it matters: This is the section’s account of how a model’s initially random parameters become useful. - Minibatch sampling tradeoff: Explain why SGD estimates the gradient using a batch instead of all training data: sampling makes each calculation quicker and gives the algorithm its “stochastic” name.
Why it matters: The choice of minibatches explains both the algorithm’s name and a practical reason it can train large models. - Gradient descent limitations: Explain why repeated gradient-guided improvements do not guarantee the best possible model: small changes can stop helping at a local minimum even if widely different parameters would produce a better model.
Why it matters: A successfully trained model is not necessarily the best model its architecture could produce. - Architecture and inference distinction: Explain why the section says training can work despite billions of random starting parameters: the architecture is tailored to the task. Distinguish the lengthy process of finding parameter values from the faster inference-time use of a model whose parameters are already set.
Why it matters: It explains why building a model is difficult even though using one can be comparatively quick. - Generative terminology distinction: Distinguish the popular use of “generative AI” for models that create content from the technical distinction presented here: a discriminative model calculates a label’s probability given input, whereas a generative model calculates the joint probability of input and label. Explain why the section places LLMs on different sides of that naming distinction.
Why it matters: The distinction helps reconcile apparently conflicting claims about whether LLMs are generative.
Key terms: stochastic gradient descent, SGD, parameters, loss, batch, minibatch, stochastic, gradient, local minimum, architecture
1.31 Convolutions (images, video, and audio)
This section explains how CNNs use learned convolutions and downsampling to process images, then briefly extends the approach to image transformation, audio, and video.
- CNN image classification pipeline: Trace how a CNN turns an image of pixels into a category prediction through repeated convolutions and downsampling, an abstract embedding, and probabilities for possible categories.
Why it matters: It connects the CNN’s processing steps to the category prediction they produce. - Learned filters and patterns: Explain what a convolutional filter can detect, how its values are chosen, and why repeated filtering can reveal increasingly complex patterns.
Why it matters: It explains how simple image features can contribute to recognizing whole objects without engineers specifying every filter. - Downsampling and object location: Justify why a CNN downsamples images when categorizing objects, relating this to its assumption about object parts and their precise locations.
Why it matters: It explains why shrinking an image can help this task rather than merely discarding useful detail. - CNNs beyond image categories: Describe the section’s image-to-image example and state how it extends the CNN processing principle to audio and video.
Why it matters: It shows that the filtering-and-abstraction approach is not limited to assigning image labels.
Key terms: CNN, convolution, downsampling, embedding, filter, learnable parameters, image categorization, parts, U-Net, image-to-image
1.32 Multimodal AI
The section explains how multimodal AI combines language and image models to connect images with text and generate images from descriptions.
- Multimodal image-to-text connection: Explain what makes an AI model multimodal and how one image-to-text approach connects a CNN to an LLM.
Why it matters: It shows how an image can be represented in a form an LLM can use. - Caption-guided diffusion training: Describe how the conditional diffusion model in this section is trained to repair images using captions.
Why it matters: This training explains why the model can use words to guide image repair. - Iterative image generation: Trace how the trained diffusion model generates a new image from a text description.
Why it matters: It connects a model trained to repair images with its ability to create one from a prompt.
Key terms: multimodal, CNN, LLM, embedding, linear projection, conditional diffusion model, U-Net, noisy image, text embedding, supervised
1.33 No free lunch
This section explains why machine learning relies on human-designed architectures tailored to particular problems rather than one universally best approach.
- Task-specific model architectures: Explain why machine learning uses architectures tailored to particular problems and what the No Free Lunch Theorem says about a universally best architecture.
Why it matters: It helps explain why different AI tasks call for different model designs. - Human-directed architecture progress: Describe how new architectures contribute to machine-learning progress and why the section says people help machines learn.
Why it matters: It counters the impression that current AI advances without substantial human design.
Key terms: No Free Lunch Theorem, architecture, transformers, CNNs, LSTMs
1.34 Summary
Where this is used later
- Misinterpretation (chapter 2) · The idea of feeding relevant documents to an LLM through retrieval-augmented generation.
- Misaligned objectives (chapter 2) · The earlier introduction to retrieval-augmented generation.
- Mitigation (chapter 2) · The earlier introduction to retrieval-augmented generation.
- Building LLM wrappers (chapter 4) · Provides cost context for building applications around LLM calls.
- Inadequate world models (chapter 2) · The discussion of contextualized embeddings as representations of token meaning.
- Model validation, selection, and testing (chapter 3) · Provides context for comparing and testing model outputs.
- Do I need to explain how the output is generated? (chapter 4) · The tradeoff introduces why the ability to explain an output can affect an AI choice.
- Toy hallucination example: Price optimization (chapter 2) · The concept of training loss.
Concepts explained in this chapter
- embeddingA vector representation used to encode the meaning of an image, text, or other content.
- language modelA neural language model trained on large text corpora to predict and generate natural language.
- transformer architectureA neural network architecture that contextualizes each token using surrounding tokens rather than compressing the whole prompt into one summary vector.
- self-supervised learningA training approach where a model learns from automatically generated targets derived from the input data itself.
- next-token predictionA language-model training objective that predicts the next token in a sequence from preceding context.
- reinforcement learning with human feedbackA training method that uses human judgments as feedback to guide reinforcement learning and improve model outputs.
- supervised learningA machine-learning approach where the model learns from labeled examples showing the desired task behavior.
- LLM wrapperSoftware that sits around an LLM and modifies inputs and outputs.
- image recognitionThe task of assigning an image to one of several possible classes based on its visual content.
- projectionA linear transformation that maps vectors into another space, used here as part of transformer attention computations.
- overfittingA failure mode in which a model becomes too specialized to its training data and generalizes poorly to new cases.
- tokenA discrete text unit fed to a language model as input, which may correspond to a subword or word rather than an individual letter.
- clusteringAn unsupervised learning method that groups similar data points into clusters.
- machine learning paradigmA broad way of framing a machine learning task, such as supervised, self-supervised, reinforcement, or unsupervised learning.
- reinforcement learningA learning approach in which an agent improves by trying actions and using feedback from the results.
- unsupervised learningA machine learning approach that seeks patterns in unlabeled data without a target output.
- machine learningA field that builds systems that learn patterns from data to perform tasks.
- convolutional neural networkA neural network architecture that applies convolutions and downsampling to learn spatial features from grid-like data such as images.
- vocabularyThe set of tokens a language model can recognize and use for tokenization and generation.
- parameterA learned model value optimized during training rather than fixed by hand.
- contextualized representationToken representations that encode surrounding context before being used to produce output predictions.
- text-to-image modelA generative AI technique that produces images conditioned on textual descriptions.
- attention scoreA numerical value used by an attention mechanism to weight the relevance of one element relative to others.
- trainingThe process of fitting a model using data and computation.
- attentionThe transformer computation that assigns attention scores and uses them to weigh contextual influence among tokens.
- feed-forward stepThe transformer update stage that further transforms contextualized embeddings after attention.
- multi-head attentionA transformer attention mechanism that splits representations into multiple heads to attend to different relationships in parallel.
- conditional diffusion modelA generative model that conditions denoising on auxiliary information such as a text caption.
- downsamplingA spatial reduction step that shrinks an image or feature map to a smaller resolution, usually discarding fine-grained location detail.
- retrieval-augmented generationA method of augmenting an LLM prompt with retrieved external documents so the model can answer using information not in its training data.
- SMOTEA data augmentation technique that generates additional minority-class examples by combining existing ones.
- data explorationIn reinforcement learning, the practice of trying random actions to gather information.
- exploitationIn reinforcement learning, the practice of choosing actions believed to maximize current reward based on known information.
- generalizationThe ability of a model to perform well on new inputs not seen during training.
- synthetic dataArtificially generated training data used as a substitute for or augmentation to real data.
- underfittingA failure mode where a model is too simple to capture the training pattern and performs poorly.
- U-NetAn encoder-decoder neural network architecture that reconstructs a full-size output image from a compressed representation, often using intermediate features from the shrinking path.
- fine-tuningAdditional training applied to an existing model to adapt or improve its behavior using new examples.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
