Stable Softmax & Temperature
Subtract the largest logit before exponentiating. Temperature changes concentration without changing rank, and equal scores share probability.
import math
logits = [1000, 1000]
weights = [math.exp(x-max(logits)) for x in logits]
print([x/sum(weights) for x in weights])From arithmetic to an experiment
Subtract the largest logit before exponentiating. Temperature changes concentration without changing rank, and equal scores share probability. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
softmax(logits, temperature) returns normalized probabilities for finite logits and temperature > 0. Return [] for empty input. Avoid overflow for large logits.
The explanation opens here once every check passes.