liveBook · Manning

Sampling

As explained in How Large Language Models Work

Sampling is the computational process of selecting output tokens from a probability distribution over vocabulary items.

Sampling is how a language model turns its output probabilities into one next token. For every vocabulary token, it estimates a probability, then randomly selects one according to those probabilities. The selected token joins the input, and the model repeats the process to generate text. section 3.2.3 section 3.5

Figure 3.10 Producing output from LLMs involves converting from documents to tokens and then using the model to produce output. We loop through this process to both consume text and generate human-readable output.
figure

Follow the loop from tokenized input through model output and back into the next generation step. section 3.2.3

Why randomness helps

Given “I love to eat,” foods such as barbeque and sushi receive high probabilities, while a car and 42 receive low probabilities. Weighted random selection might choose “tacos” rather than always choosing the single most likely food. Generation continues until the model produces an end-of-sequence token or reaches a maximum limit. section 3.2.3

This randomness explains why the same prompt can produce different answers. Increasing or reducing its strength trades consistency against creativity and variety; randomness can improve natural-sounding text but does not guarantee accuracy. section 3.2.3 section 3.5

How How Large Language Models Work covers it

How deep it goes
Traces probability calculation, weighted selection, repetition, randomness, and stopping without detailing sampling variants.
Examples
Uses “I love to eat,” weighted food-token choices, repeated prompts, and an end-of-sequence stopping example.
What you can do after
Evaluate why outputs vary and adjust randomness when choosing between more consistent or more creative generation.
How it connects
Connects sampling with autoregressive models, unembedding, probability distributions, randomness, and end-of-sequence tokens.

Also explained in

Compare how 23 books explain sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.