liveBook · Manning

Temperature

As explained in Transformers in Action

Temperature sampling is a sampling strategy that adjusts the sharpness of a token probability distribution before selecting the next token.

Temperature sampling adjusts token probabilities before selection, controlling the trade-off between focused, predictable text and diverse, random text. section 4.1.5

The temperature modifies logits before softmax. Values below 1 sharpen probabilities around likely tokens; values above 1 flatten them and increase randomness. Temperature 1 leaves probabilities unchanged. section 4.1.5

Use temperature to tune generation behavior:

  • Choose a low temperature for deterministic, focused output approaching greedy decoding. section 4.1.5
  • Choose a high temperature for more varied output and exploration of less likely tokens. section 4.1.5
  • Combine temperature with methods such as top-p to balance coherence and novelty. section 4.3

Transformers generation

Enable sampling in model.generate(), set temperature to 0.7, request 128 new tokens, and return three sequences. Changing the temperature changes the diversity and focus of the generated continuations. section 4.1.5

Listing 4.5 Temperature sampling implementation
temperature_outputs = model.generate(
    input_ids=input_ids,
    max_new_tokens=128,
    do_sample=True,
    temperature=0.7,
    num_return_sequences=3,
    pad_token_id=tokenizer.eos_token_id
)

Inspect the executable temperature-sampling configuration, including sampling activation, temperature, sequence length, and multiple returned sequences. section 4.1.5

How Transformers in Action covers it

How deep it goes
Explains logit adjustment, softmax sharpness, limiting behavior, and a Transformers implementation, without deriving the formula.
Examples
Uses a Transformers generate() listing with temperature 0.7, three returned sequences, and a generated AI-researcher continuation.
What it assumes
Assumes familiarity with token sampling, logits, softmax probabilities, and transformer text generation.
What you can do after
Apply it via Transformers to produce more focused or more diverse text continuations.
How it connects
Connects temperature sampling to decoding, greedy search, top-k sampling, nucleus sampling, softmax, and prompting.

Also explained in

Compare how 17 books explain temperature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.