liveBook · Manning

Top-k sampling

As explained in Deep Learning with R, Third Edition

Top-k sampling selects the next token from only the k highest-scoring candidate tokens and assigns zero probability to the rest.

Top-k sampling generates text by choosing the next token from only the k highest-scoring candidates; all other tokens are excluded. It offers a middle ground between always choosing the most likely token and sampling from every token. section 16.2.4

Top-k and temperature sampling shape token selection in different ways:

  • Top-k excludes candidates outside the top k; temperature changes how likely tokens are without excluding any. section 16.2.4
  • The two can be combined, such as sampling from the top five candidates with a temperature of 0.5. section 16.2.4
top_k <- function(preds, k = 5, temperature = 1) {
  preds <-  preds / temperature
  .[top_preds, top_indices] <- op_top_k(preds, k = k, sorted = FALSE)
  choice <- random_sample(top_preds)
  op_take(top_indices, choice)
}

The R function scales predictions by temperature, selects the top k values with op_top_k(), then samples among them. section 16.2.4

Changing k

With the same prompt, examples using k = 5 and k = 20 produce different generated passages, illustrating how the candidate cutoff shapes output. section 16.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through candidate selection, temperature interaction, and generated outputs, without deriving sampling mathematically.
Examples
Compares generated passages with k values of 5 and 20 and with top-k combined with temperature 0.5.
What you can do after
Apply it via op_top_k() to restrict candidate tokens and sample among them, optionally adjusting temperature.
How it connects
Connects top-k sampling to categorical distributions, greedy search, random sampling, temperature sampling, and beam search.

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