Concept units

Softmax: probabilities over many choices

Intermediate

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.

Softmax turns scores into shares of a whole

Picture a model that looks at a photo and must say whether it shows a cat, a dog or a bird. It gives each choice, or class, a score: say 2.0 for cat, 1.0 for dog and 0.1 for bird. These scores rank the classes, but they are not probabilities. A score can be negative, and the three do not add up to 1.

Softmax fixes both problems in two steps. First, raise e ≈ 2.718 to each score. The exponentials unit shows that ez is positive for every z. Second, divide each result by the sum of all of them. For class i out of n classes:

pi = ezi / ( ez1 + ez2 + … + ezn )

Predict first. The cat's score is 1 higher than the dog's. Will the cat be twice as likely as the dog, or more, or less?

Worked example: cat 2.0, dog 1.0, bird 0.1
  1. Raise e to each score: e2.0 ≈ 7.389, e1.0 ≈ 2.718, e0.1 ≈ 1.105.
  2. Add them up: 7.389 + 2.718 + 1.105 = 11.212.
  3. Divide each by the total: cat 7.389 / 11.212 ≈ 0.659, dog 0.242, bird 0.099.

The three probabilities add up to 1. The cat is 0.659 / 0.242 ≈ 2.72 times as likely as the dog. That ratio is e1, because the cat's score is 1 higher.

Raising one score takes probability from every other class
class score probability cat2.0 0.659 dog1.0 0.242 bird0.1 0.099

scores 2.0, 1.0, 0.1 → probabilities 0.659, 0.242, 0.099 (sum 1.000)

Try it. Drag the dog's score until the dog and the cat are equally likely. What score is that, and what is left for the bird? Then predict what "Add 5 to every score" will do to the bars before you press it.

What to notice. The bars always fill exactly one whole between them. When the dog's score rises, the cat's and the bird's bars both shrink. Adding 5 to every score changes the scores but not one bar.

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]
Real code subtracts the largest score first

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Where this shows up

Units that build on this one