liveBook · Manning

Sampling

As explained in Natural Language Processing in Action, Second Edition

Sampling is a decoding method that selects the next token probabilistically from a distribution over candidate tokens.

Sampling selects the next token probabilistically from the distribution conditioned on tokens already generated. Unlike greedy decoding, it may choose the second- or third-most-probable token, so repeated inference runs can produce different sentences. section 10.2

Temperature controls how often decoding moves farther down the probability ranking: higher variation can produce creative but nonsensical text, while constrained choices reduce unpredictability. Sampling complements greedy search, repetition filtering, and beam search as a token-decoding strategy. section 10.2 section 10.2.1

Nucleus sampling with GPT-2

A runnable Hugging Face workflow loads GPT-2, tokenizes a prompt, generates text, and decodes the result. Setting do_sample=True and top_p=0.92 enables nucleus sampling, which restricts choices to a probability nucleus. For the prompt “NLP is a”, one output continues with an improvised explanation of network protocols, illustrating both fluent structure and factual unreliability. section 10.2.1

Figure 10.2 Stochastic chameleons deciding words one at a time
figure

A fishbone-style decision tree makes stochastic token selection visible by showing ranked alternatives and the chosen word at each generation step. section 10.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains conditional token probabilities, ranking, temperature, and nucleus-sampling configuration, while simplifying the full branching process.
Examples
Uses GPT-2 inference, ranked next-token probabilities, a fishbone decision tree, and nucleus sampling for the prompt “NLP is a”.
What it assumes
Assumes a pretrained causal language model and tokenization, then demonstrates inference from token probabilities to generated text.
What you can do after
Apply it via Hugging Face Transformers by enabling sampling and configuring top_p during GPT-2 text generation.
How it connects
Connects sampling with token decoding, temperature, greedy search, repetition filtering, beam search, and nucleus sampling.

Also explained in

Compare how 23 books explain sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.