Softmax: probabilities over many choices
Read these first if they are new to you
The idea in one paragraph
A model that picks one of several choices usually gives each choice a score: any number, where higher means more likely. Softmax turns the list of scores into a list of probabilities. It raises e to each score, which makes every value positive, then divides by the total, which makes the values add up to 1. The highest score gets the largest share. Only the gaps between scores matter: adding the same number to every score changes nothing.
Only the gaps between scores matter
Divide the formula for one class by the formula for another. The totals cancel, and what is left is
pi / pj = ezi − zj
So each extra point of score gap multiplies the ratio of the two probabilities by e ≈ 2.718. Three things follow:
- Adding the same number to every score leaves every probability unchanged, because no gap changes.
- Doubling every score doubles every gap, so the largest class takes a bigger share. Scores 4.0, 2.0 and 0.2 give 0.864, 0.117 and 0.019.
- Halving every score shrinks the gaps, so the shares even out. Scores 1.0, 0.5 and 0.05 give 0.502, 0.304 and 0.194.
When the gaps are large, almost all the probability goes to the top score. That is why the function is called a "soft" maximum: it behaves like picking the largest score, but smoothly. Some code divides every score by a number called the temperature before the softmax, to make the shares sharper or flatter on purpose.
With exactly two classes, softmax is the sigmoid in disguise. Give one class score z and the other 0. Then the first class gets ez / (ez + 1) = 1 / (1 + e−z) = σ(z).
softmax.py
import numpy as np
def softmax(z):
e = np.exp(z - z.max()) # subtract the largest score first
return e / e.sum()
print(np.round(softmax(np.array([2.0, 1.0, 0.1])), 3))
print(np.round(softmax(np.array([12.0, 11.0, 10.1])), 3))
print(np.round(softmax(np.array([1000.0, 999.0])), 3))
[0.659 0.242 0.099] [0.659 0.242 0.099] [0.731 0.269]
The formula as written breaks for large scores. e1000 is too large for a computer's numbers, so a direct computation of softmax([1000, 999]) divides infinity by infinity. Because adding a constant changes nothing, code subtracts the largest score first. Every exponent is then 0 or less, and the answer is the same. The second and third lines of output above show both facts.
Softmax also guarantees only the form of a probability: positive shares that add up to 1. Whether a share of 0.9 is right 9 times in 10 depends on training.
Softmax also appears inside networks, where it turns similarity scores into weights. The attention unit covers that use. To train a model whose output is a softmax, the usual loss is cross-entropy.
Check yourself
-
Work the softmax of the scores 0, 1 and 2 by hand.
Answer
e0 = 1, e1 ≈ 2.718, e2 ≈ 7.389. The total is 11.107. The probabilities are 0.090, 0.245 and 0.665.
-
All three classes score 3. What does softmax give? What if all three score −50?
Answer
One third each, both times. Equal scores have no gaps, so the shares are equal, whatever the common value.
-
Class A scores 2 higher than class B. How many times more likely is A than B?
Answer
e2 ≈ 7.39 times, whatever the other scores are.
-
Can softmax ever give a class a probability of exactly 0?
Answer
Not in exact arithmetic: ez is positive for every score. A computer can round a tiny share to 0, for example when one score is hundreds below the top score.
Go further
Optional background, for readers who want more than this page.
-
Multi-class neural networks: one versus one (softmax) Google Machine Learning Crash Course Course page Intermediate
A short section that places softmax as the last layer of a classifier, with a diagram.
-
The textbook treatment, with the formula, its link to classification and runnable code.
-
Clear notes that end with a practical note on numerical stability, the same trick as above.
Where this shows up
Units that build on this one
- Cross-entropy: scoring a choice among many Intermediate
- Attention: queries, keys, values Advanced