liveBook · Manning

Temperature

As explained in Deep Learning with R, Third Edition

Temperature is a generation parameter that controls the concentration or spread of a model's next-token probability distribution.

Temperature controls how concentrated a model’s next-token probability distribution is during sampling. Lower values favor the most likely tokens; higher values spread probability across more alternatives. section 16.2.4

Changing temperature

With temperature 0.2, generation begins to resemble greedy search and repeats patterns. At 0.8, it produces more coherent variation. At 2, it becomes highly diverse and may produce seemingly random text. section 16.2.4

Implementation divides the model’s logits by temperature before reshaping them and sampling with random_categorical(). The resulting value changes exploration without removing any token from consideration. section 16.2.4

Temperature differs from top-k sampling: temperature changes relative likelihoods, whereas top-k sets probabilities outside the selected candidates to zero. Both strategies can be combined. section 16.2.4

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

A piece of advice, just read my knees and stick with getables, and a hello to

me. However, the bar napkin doesn’t last as long. I happen to be waking up close

and pull it up as I wanted too and I still get it, really, shouldn’t be a 

reaction)

Inspect a complete random-sampling function and compare generated text at the default temperature. section 16.2.4

compiled_generate(prompt, \(preds) top_k(preds, k = 5, temperature = 0.5))

A piece of advice that you can use to get rid of the problem.

The first thing you need to do is to get the job done. It is important that you

have a plan that will help you get rid of it.

The first thing you need to do is to get rid of the problem yourself.

Compare combined top-k and temperature sampling, including a lower-temperature output. section 16.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains logits, softmax concentration, generated-output effects, and combination with top-k, without deriving the probability transformation.
Examples
Uses generated text at temperatures 0.2, 0.8, and 2, plus combined top-k examples with k values of 5 and 20.
What it assumes
Assumes model predictions and token generation, while demonstrating sampling from a categorical distribution rather than requiring formal probability derivations.
What you can do after
Apply it via a random sampling function and tune generation between repetitive low-temperature output and erratic high-temperature output.
How it connects
Connects temperature with logits, softmax, categorical distribution, random sampling, greedy search, and top-k sampling.

Also explained in

Compare how 17 books explain temperature →

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.