Core AI Intuitions · lesson 02/4
Softmax Probabilities
A classifier's last layer usually emits raw scores called logits, not probabilities. Softmax turns any vector of real numbers into a probability distribution, and it is also what converts attention scores into the mixing weights used to build a context vector.
The idea
For a vector with components, softmax assigns
Every output is positive and the outputs sum to exactly , so the result is a valid distribution over the options. Three properties carry most of the intuition:
- Order preserving — a larger logit always yields a larger probability, so
argmaxis unchanged from the logits to the probabilities. - Shift invariance — adding the same constant to every logit cancels top and bottom, because . Only differences between logits matter.
- Exponential gaps — a one-unit logit gap becomes a probability ratio of , so softmax amplifies small score differences into large confidence differences.
The temperature controls sharpness: with small approaches a one-hot argmax, while large flattens toward the uniform distribution.
Worked example
Let . Exponentiating gives with sum , so
The top logit is only above the runner-up, yet it receives about the probability. With the logits become and the distribution flattens to .
In code
import torch
z = torch.tensor([1.0, 2.0, 3.0])
p = torch.softmax(z, dim=0)
print(p) # tensor([0.0900, 0.2447, 0.6652])
print(p.sum()) # tensor(1.)
print(torch.softmax(z / 2, dim=0)) # tensor([0.1863, 0.3072, 0.5065])
# Stability: softmax subtracts the max before exponentiating.
print(torch.softmax(torch.tensor([1000.0, 1000.0]), dim=0)) # tensor([0.5, 0.5])
print(torch.exp(torch.tensor(1000.0))) # tensor(inf) -> don't do thisCheck yourself
- Why does adding
100to every logit leave the softmax output unchanged? - If lowering the temperature makes the distribution sharper, what happens to it as
Tgrows very large? - Why is cross-entropy loss computed on logits rather than on probabilities that have already been passed through softmax?
Key takeaways
- Softmax maps unnormalized logits to positive weights that sum to one.
- Only logit differences matter, and those differences are amplified exponentially.
- Temperature tunes how sharp or flat the resulting distribution is.