Skip to content

← Decoding Strategies step 2 of 8

Easy Research

Temperature Scaling

Temperature scaling changes how concentrated a softmax distribution is. Divide the model's logits by one positive temperature, then apply softmax over the classes for each example.

Your task

Implement temperature_scale(logits, temperature).

  • logits has shape (batch, n_classes), with at least one class per row.
  • temperature is a finite scalar T > 0, shared across all examples and classes.
  • Inputs are finite floating-point values; the scaled logits must remain representable in the input dtype.

Return probabilities of shape (batch, n_classes), preserving the logits' dtype and leaving the input unchanged. For each row, compute:

scaled = logits / temperature
shifted = scaled - max(scaled)
probabilities = exp(shifted) / sum(exp(shifted))

The maximum and sum are over the class axis, separately for each row. Subtracting the maximum makes softmax numerically stable without changing its mathematical result. Return probabilities, not scaled logits or class IDs.

For logits=[[2, 0]] and temperature=2, the scaled row is [1, 0] and the result is approximately [[0.731059, 0.268941]]. Each row sums to one, up to floating-point rounding.

What changes with temperature?

  • T=1 gives the ordinary softmax of the input logits.
  • T>1 makes the distribution less concentrated.
  • 0<T<1 makes it more concentrated.

Equal logits always produce a uniform distribution. A positive temperature preserves class ranking, so the highest-logit class does not change in exact arithmetic. Do not divide probabilities by temperature or normalize across the batch.

Scaling versus calibration

Guo et al. (2017) study temperature scaling as a calibration method. Here the temperature is supplied: you do not fit it, change model weights or evaluate calibration. Applying an arbitrary temperature does not by itself guarantee well-calibrated probabilities.