liveBook · Manning

Top-p filtering

As explained in How Large Language Models Work

Nucleus sampling is a token-generation method that samples only from the smallest set of highest-probability tokens whose cumulative probability reaches a selected threshold.

Top-p filtering, also called nucleus sampling, narrows token generation to the smallest group of high-probability tokens whose combined probability reaches a chosen threshold. Sampling from this group can reduce unreasonable predictions while preserving some variety. section 3.2.3

Why the choice matters

After “I love to eat,” foods such as barbecue and sushi receive high probabilities, while a car or 42 receive low probabilities. A rare choice such as “chalk” may still lead the model into a medically themed tangent about pica. section 3.2.3

Figure 3.11 We demonstrate text generation by starting with the phrase "I love to eat" and then showing that some possible completions that are foods, such as barbeque and sushi, have high proba-bilities, while a car and the number 42 have low probabilities. Weighted random selection chooses the word tacos. The generation loop is stopped when the EoS token appears.
figure

Trace how token probabilities guide weighted selection during a generation loop, ending when the end-of-sequence token appears. section 3.2.3

How How Large Language Models Work covers it

How deep it goes
Defines the selection strategy and its motivation but does not explain threshold tuning or implementation details.
Examples
Uses food completions after “I love to eat” and the unusual “chalk” continuation into pica.
What you can do after
Evaluate generation risks by checking which sampling algorithm an LLM uses.
How it connects
Connects token generation with probability, weighted random selection, and end-of-sequence tokens.

Also explained in

Compare how 12 books explain top-p filtering →

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.