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 : 0 when one token takes everything, and at its largest, log V, when every token is equal.
Next token after “The cat sat on the ___”
- 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.
is the variance of the logits, weighted by the tempered probabilities . 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.