liveBook · Manning

Top-k sampling

As explained in Deep Learning with Python, Third Edition

A decoding method that limits token selection to the K most probable candidates before sampling.

Top-k sampling generates text by restricting each next-token choice to the K most probable candidates, then sampling from that smaller set. It sits between greedy decoding, which always chooses one token, and unrestricted random sampling, which considers every token. Unlike temperature, top-k sampling gives tokens outside the cutoff zero probability; both controls can be combined. section 16.2.4

Changing K changes the output

With the same prompt, K=5 produces relatively plausible but repetitive prose, while K=20 allows more varied continuations. Combining K=5 with temperature 0.5 produces more focused, plan-like repetition. As a rule of thumb, smaller K narrows the model's choices; larger K preserves more variety. section 16.2.4

A practical implementation follows three steps:

  • Scale the predicted logits by the selected temperature. section 16.2.4
  • Use keras.ops.top_k to retain the K highest-scoring candidates and their indices. section 16.2.4
  • Sample from the retained scores, then map the sampled position back to its original token index. section 16.2.4
def top_k(preds, k=5, temperature=1.0):
    preds = preds / temperature
    top_preds, top_indices = ops.top_k(preds, k=k, sorted=False)
    choice = keras.random.categorical(top_preds[None, :], num_samples=1)[0]
    return ops.take_along_axis(top_indices, choice, axis=-1)

The Keras implementation combines temperature scaling, top-k selection, categorical sampling, and index lookup in one decoding function. section 16.2.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Implements top-k decoding in Keras and compares it with temperature, greedy, random, and beam-search strategies.
Examples
Demonstrates K=5, K=20, and K=5 with temperature 0.5 through generated text continuations.
What it assumes
Assumes next-token prediction and categorical sampling, while distinguishing top-k filtering from temperature scaling.
What you can do after
Apply it via Keras to generate text with adjustable candidate-set size and temperature.
How it connects
Connects categorical distributions with temperature sampling, greedy search, random sampling, beam search, and next-token prediction.

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 Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.