liveBook · Manning

Top-k sampling

As explained in Machine Learning Platform Engineering

Top-K sampling generates each token by restricting selection to the K most probable next tokens.

Top-k sampling generates each token by restricting selection to the k most probable next tokens. It introduces controlled randomness while excluding very unlikely choices that could derail a response. section 12.1.1

Use the parameter to adjust determinism and variety:

  • Set top_k=1 for deterministic output. section 12.1.1
  • Set top_k=40 to balance variety and quality. section 12.1.1
  • Compare top-k with temperature and top-p when matching randomness to the application. section 12.1.1

Output-randomness configuration

A configuration can test top_k=1, top_k=40, and top_k=100 alongside temperature and top_p settings. Lower k narrows the candidate vocabulary; higher k permits more alternatives. section 12.1.1

How Machine Learning Platform Engineering covers it

How deep it goes
Defines candidate restriction and parameter effects, but does not explain sampling internals or implementation algorithms.
Examples
Uses top_k=1, top_k=40, and top_k=100 to illustrate deterministic, balanced, and broader candidate selection.
What you can do after
Apply top-k sampling by selecting a candidate-set size that matches the desired consistency, variety, and coherence.
How it connects
Contrasts top-k sampling with temperature and top-p sampling while operating over next-token probabilities.

Also explained in

Compare how 8 books explain top-k sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.