8/29
Part 2 of 6 · TemperatureLearn and do

A higher temperature always spreads the weight more

How spread out is a distribution? Entropy puts a number on it.

def bits(p: torch.Tensor) -> float:
    """p: probabilities that sum to 1. Returns their entropy in bits."""
    return -(p * p.log2()).sum().item()

for T in (0.5, 1.0, 2.0):
    print(T, round(bits((row / T).softmax(dim=-1)), 2))
0.5 0.71
1.0 1.66
2.0 2.29

Entropy is H=−∑ipilog⁡piH = -\sum_i p_i \log p_i: 0 when one token takes everything, and at its largest, log V, when every token is equal.

Entropy against temperatureInteractive
Next token after “The cat sat on the ___”
012all 6 equal: 2.58 bits1.66 bits00.511.52temperatureentropy, in bits
Tokens kept
6 of 6
Top token
0.60
Entropy
1.66 bits

Entropy rises with T and never falls. If asked why: the slope is the variance of the logits divided by T³, and neither can be negative.

dHdT=Var⁡p(z)T3≥0\frac{dH}{dT} = \frac{\operatorname{Var}_p(z)}{T^3} \ge 0

Var⁡p(z)\operatorname{Var}_p(z) is the variance of the logits, weighted by the tempered probabilities pp. The slope is exactly 0 only when every logit is the same. (The formula uses log base e, so it counts in nats. One nat is about 1.44 bits.)

In the editor, write tempered_probs(logits, temperature): (B, V) logits in, the probabilities at that temperature out. At temperature == 0, put all of each row's weight on its argmax.