WRITE · TRACE · UNDERSTAND

Select temperature on a held-out calibration split

Evaluate stable mean multiclass log loss for each distinct positive finite temperature in grid.

Read lesson: Select temperature on a held-out calibration split →

Your experiment

Evaluate stable mean multiclass log loss for each distinct positive finite temperature in grid. Divide logits by temperature; choose minimum loss with smaller temperature breaking exact ties. Return temperature and loss. Require aligned nonempty rectangular finite logits with >=2 classes and integer target identities. These inputs are calibration data, never training or final test labels.

Python runs in your browser. Your draft stays on this device.

What to look for

Keep calibration, model development and final evaluation populations separate. Inspect subgroup calibration and proper scoring rules rather than only classification accuracy.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Select temperature on a held-out calibration split works

The idea

Temperature scaling repairs overconfident probabilities without changing predictions. Every logit is divided by one positive number T before the softmax. A T above 1 flattens the distribution and a T below 1 sharpens it. Dividing by a positive constant keeps the order of the logits, so the predicted class never changes.

fit_temperature(logits, labels, grid) picks T by trying each distinct value in grid and computing the mean log loss with the same stable log-sum-exp used for cross-entropy. The lowest loss wins, and an exact tie goes to the smaller temperature. The data must be a held-out calibration split. Training rows are fitted too well to reveal overconfidence, and using test labels would make the final test report optimistic.

How it works

  1. Validate the grid (positive finite values) and the data (aligned, rectangular, finite logits with at least two classes and integer targets).
  2. Loop over the distinct temperatures in ascending order.
  3. Divide every logit in a row by the temperature to get scaled.
  4. Compute the row's log loss as log(sum(exp(x - peak))) + peak - scaled[target], where peak is the row maximum.
  5. Average the row losses and store the pair (loss, temperature) in scores.
  6. Take the minimum pair, which prefers lower loss and then the smaller temperature, and return both values.

What it costs

Time
O(g·n·c)
Space
O(n + g)

Each of the g grid temperatures rescans all n rows of c logits, and one loss per temperature is kept.

Where it is used

  • Guo and co-authors proposed temperature scaling in 2017, fitting one scalar on a validation set by minimising negative log likelihood.
  • Knowledge distillation, introduced by Hinton and co-authors, divides teacher logits by a temperature to produce softer targets.
  • Text generation APIs expose a temperature setting that divides the next-token logits before sampling.

Watch out for

  • Fitting the temperature on the training set, where losses are already low, so the chosen T understates the correction needed.
  • Expecting accuracy to change: dividing by a positive T never reorders the logits, only the confidence.
  • Allowing a temperature of zero or below, which divides by zero or flips the ranking; the Lab rejects it.