One Temperature for the Batch

Loss FunctionsHard+30 XP+30

One Temperature for the Batch

The softmax lesson mentioned an optional dial called temperature. Every logit is divided by the same number T>0T > 0T>0 before softmax:

pj=ezj/T∑kezk/Tp_j = \frac{e^{z_j / T}}{\sum_k e^{z_k / T}}pj​=∑k​ezk​/Tezj​/T​

T=1T = 1T=1 is plain softmax. A smaller TTT sharpens the probabilities toward the top class, and a larger TTT flattens them toward an even split. Dividing every logit by the same positive number never changes which one is largest, so the model's answers are identical at every temperature. Only its confidence moves.

A finished classifier has scored four photos. Each row holds its logits for three animals:

photocatdogfoxtrue animal
14.00.5−1.0cat
23.51.00.0cat
30.04.50.5dog
45.00.01.0fox

The loss is categorical cross-entropy. The loss on one photo is −ln⁡ptrue-\ln p_{\text{true}}−lnptrue​, where ptruep_{\text{true}}ptrue​ is the probability given to its true class and ln⁡\lnln is the natural logarithm. The batch loss is the average over the photos.

Task: write best_temperature(batch_logits, true_classes), returning [temperature, loss_at_one, loss_at_best], each rounded to 4 decimal places:

  • temperature is the value of TTT that gives the lowest batch loss;
  • loss_at_one is the batch loss at T=1T = 1T=1;
  • loss_at_best is the batch loss at that best temperature.

Details:

  • batch_logits[i] is the list of logits for photo i, one per class, and true_classes[i] is the index of its true class. Every photo has the same number of classes, at least 222.
  • Near T=0T = 0T=0 each photo becomes certain of its top class, so any photo whose true class is not on top costs without limit. Far above 111 every photo drifts toward an even split. Every test batch is built so that, as TTT sweeps upward from near 000, the batch loss falls to a single lowest point and then rises again.
  • Answers must be exact to 4 decimal places, which a grid of temperatures will not reliably reach.

The four photos above are the first test, with true_classes equal to [0, 0, 1, 2].

A best temperature above 1 means the model is overconfident: its mistakes are held too firmly, and the batch pays less once every photo is made more modest. A best temperature below 1 means it is timid about answers it mostly gets right. The answers never change either way, which is why accuracy cannot see any of this, and why cross-entropy, which grades confidence, can.

Examples

batch_logits = [[4.0, 0.5, -1.0], [3.5, 1.0, 0.0], [0.0, 4.5, 0.5], [5.0, 0.0, 1.0]]
true_classes = [0, 0, 1, 2]
returns [2.2838, 1.0491, 0.7493]
batch_logits = [[0.6, 0.1, -0.2], [0.2, 0.9, 0.0], [-0.1, 0.1, 0.7], [0.4, 0.3, 0.0]]
true_classes = [0, 1, 2, 1]
returns [0.1538, 0.7756, 0.3]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run