liveBook · Manning

Keras.random.categorical()

As explained in Deep Learning with R, Third Edition

random_categorical() is a Keras operation that samples category indices from categorical probability distributions.

Samples a category index from a categorical distribution, allowing text generation to choose among possible next tokens instead of always taking the most likely one. section 16.2.4

Sample the next token

random_sample <- function(preds, temperature = 1) { preds <- preds / temperature preds <- op_reshape(preds, c(1, -1)) random_categorical(preds, num_samples = 1) |> op_squeeze() } compiled_generate(prompt, random_sample)

Control the sampling behavior with:

  • Temperature: lower values sharpen the distribution toward likely categories; higher values spread probability across more categories. section 16.2.4
  • Top-k sampling: restricts random choice to a selected number of likely categories before sampling. section 16.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains softmax-based random selection and temperature effects, then contrasts them with greedy and top-k strategies.
Examples
Demonstrates R functions for random sampling, temperature changes, and top-k generation with generated text outputs.
What it assumes
Assumes model predictions and token-generation code while demonstrating sampling from zero.
What you can do after
Apply it via a sampling function to generate more varied text and tune exploration with temperature.
How it connects
Connects categorical distributions with softmax, temperature, greedy search, top-k sampling, and Transformer predictions.

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.