Maths toolkit15 / 17

Lesson 15 · Probability and information

Scores to probabilities: argmax, softmax and log-probabilities

The problem

A pizza shop wants to guess what the next customer will order. A model looks at the customer and gives each topping a score:

ToppingScore
pepperoni2
margherita1
veggie0
hawaiian−1

A higher score means "more likely". But the scores aren't probabilities. They can be negative, and they don't add up to 1.

You can ask two different questions about these scores:

  1. Which topping is most likely? Pepperoni, the biggest score.
  2. How likely is each topping? For that, we need to turn the scores into probabilities.

The first question is argmax. The second is softmax. Neural networks produce scores like these all the time. Their raw output scores are called logits, and softmax is how they become probabilities.

Argmax: which is biggest?

The argmax of a list is the position of its biggest entry. Counting from 0, as Python does:

arg⁡max⁡ (2, 1, 0, −1)=0(pepperoni)\arg\max\,(2,\ 1,\ 0,\ -1) = 0 \quad\text{(pepperoni)}

Compare it with max, which gives the biggest value: max⁡ (2,1,0,−1)=2\max\,(2, 1, 0, -1) = 2. Argmax answers "which one?", and max answers "how big?".

Argmax is all or nothing. Pepperoni wins, and the other toppings get nothing, even if they came close. If margherita's score rose from 1 to 1.99, argmax wouldn't change at all. If it rose to 2.01, argmax would jump to margherita.

Softmax: how likely is each?

How could we turn (2,1,0,−1)(2, 1, 0, -1) into probabilities? Probabilities must be zero or more, and they must add up to 1.

Predict first

Could we divide each score by the total of the scores?

The fix is to make every score positive first, in a way that keeps their order. The exponential eze^z does that. It's always positive, and a bigger zz always gives a bigger eze^z. Then divide by the total.

Worked example

Softmax of the scores (2, 1, 0, −1)

  1. Take the exponential of each score: e2=7.389e^{2} = 7.389, e1=2.718e^{1} = 2.718, e0=1e^{0} = 1, e−1=0.368e^{-1} = 0.368. All positive.

This recipe is softmax. For a list of scores z=(z1,…,zn)\mathbf{z} = (z_1, \dots, z_n):

softmax(z)i=ezi∑j=1nezj\mathrm{softmax}(\mathbf{z})_i = \frac{e^{z_i}}{\sum_{j=1}^{n} e^{z_j}}

Read it as: the probability of option ii is its exponentiated score, as a share of all the exponentiated scores. The bottom line is the same for every ii. It's there only to make the results add up to 1.

Drag the scores below. The table shows each step of the recipe. The two sets of bars compare argmax with softmax.

toppingscoreexpprob
pepperoni2.07.390.64
margherita1.02.720.24
veggie0.01.000.09
hawaiian-1.00.370.03

argmax: all or nothing

  • pepperonipick
  • margherita–
  • veggie–
  • hawaiian–

softmax: a probability for each

  • pepperoni0.64
  • margherita0.24
  • veggie0.09
  • hawaiian0.03

prob = exp(score) ÷ 11.48, the total of the exp column. Argmax = 0 (pepperoni).

Argmax gives everything to the top score. Softmax gives every topping a share, and bigger scores get bigger shares. The buttons add or subtract 1 from every score: watch what happens to the probabilities.

Notice two things:

  • Each 1 point of score difference multiplies the probability by e≈2.718e \approx 2.718. Pepperoni is 1 point above margherita, so it's 0.644/0.237≈2.7180.644 / 0.237 \approx 2.718 times as likely.
  • Make two scores equal and their probabilities become equal too.

Predict first

Add 10 to every score, giving (12, 11, 10, 9). What happens to the softmax probabilities?

So a score on its own means nothing. Only the gaps between scores matter. Later in this lesson, that fact keeps very large scores manageable.

DeeperTwo options: softmax is the sigmoidWith only two scores, softmax becomes the S-shaped curve used for yes/no questions. · 2 min

Take two scores, z1z_1 and z2z_2. Since only the gap matters, subtract z2z_2 from both: (z1−z2, 0)(z_1 - z_2,\ 0). Call the gap x=z1−z2x = z_1 - z_2. Then:

softmax(x, 0)1=exex+e0=exex+1=11+e−x\mathrm{softmax}(x,\ 0)_1 = \frac{e^{x}}{e^{x} + e^{0}} = \frac{e^{x}}{e^{x} + 1} = \frac{1}{1 + e^{-x}}

The last step divides top and bottom by exe^{x}. That curve is the sigmoid. It turns one score into a probability between 0 and 1.

−6−4−2024600.20.40.60.81gap x = z₁ − z₂probability of option 1

gap x = 1 → 1 / (1 + e−x) = 0.731

softmax(x, 0) gives the same: 0.731. Drag the dot or use the arrow keys.

The sigmoid: the probability of option 1 as the gap between two scores changes. A gap of 0 gives 0.5; big gaps push it towards 0 or 1.

For example, a gap of x=1x = 1 gives 1/(1+e−1)=1/1.368=0.7311 / (1 + e^{-1}) = 1 / 1.368 = 0.731. A gap of 0 gives exactly 0.5. Models that answer yes/no questions use the sigmoid. It's softmax with two options in disguise.

Temperature: how decisive?

Sometimes you want the model to be bolder, or more open-minded. A single dial does this. Divide every score by a number τ\tau (the Greek letter tau) before the softmax:

pi=softmax ⁣(zτ)i=ezi/τ∑jezj/τp_i = \mathrm{softmax}\!\left(\frac{\mathbf{z}}{\tau}\right)_i = \frac{e^{z_i / \tau}}{\sum_j e^{z_j / \tau}}

The number τ\tau is the temperature. With τ=1\tau = 1 it's plain softmax.

Predict first

Set τ = 0.5. Each score is divided by 0.5, so the scores become (4, 2, 0, −2). Does pepperoni's probability go up or down from 0.644?

Slide the temperature. The slider moves on a log scale, so 0.1, 1 and 10 are evenly spaced.

Scores ÷ τ: (2, 1, 0, −1)

softmax(scores ÷ 1)

  • pepperoni0.64
  • margherita0.24
  • veggie0.09
  • hawaiian0.03

Biggest probability: 0.644 (pepperoni)

Dashed lines: an even split, 0.25. τ = 1 is plain softmax.

Low temperature sharpens the probabilities until nearly all of it sits on the top score, like argmax. High temperature flattens them towards an even split. The ranking never changes.

Here are the numbers for the topping scores:

τ\taupepperonimargheritaveggiehawaiian
0.250.9820.0180.00030.000006
0.50.8650.1170.0160.002
10.6440.2370.0870.032
20.4550.2760.1670.102
100.2890.2610.2360.214

As τ\tau shrinks towards 0, the gaps grow without limit. The top score takes all the probability, and softmax becomes argmax. That's where the name comes from: softmax is a "soft" version of argmax. As τ\tau grows, every score shrinks towards 0, and the probabilities approach an even split of 0.25 each.

Temperature comes up all through the book:

  • In reinforcement learning, a policy can pick actions with softmax over their scores. Low temperature means "mostly pick the best-looking action". High temperature means "explore more".
  • In language models, sampling picks the next word from softmax probabilities. Low temperature gives safe, repetitive text. High temperature gives surprising, sometimes nonsensical text.
  • In transformers, attention uses softmax to decide how much each word looks at each other word.

Log-probabilities

Many small probabilities

Say the model gives each order its softmax probability. What's the chance of a whole run of orders? If the orders are independent, multiply their probabilities. For pepperoni then margherita, that's 0.644×0.237=0.1530.644 \times 0.237 = 0.153.

Now take 200 orders: a fixed pattern of 10 orders, repeated 20 times. Each probability is below 1, so the product shrinks fast:

OrdersProduct of probabilitiesSum of log-probabilities
10.644−0.44
20.153−1.88
105.6×10−75.6 \times 10^{-7}−14.40
1002.8×10−632.8 \times 10^{-63}−144.02
2008.1×10−1268.1 \times 10^{-126}−288.04

The product has 125 zeros after the decimal point. Numbers like that are hard to read and hard to compare.

The fix is to add logs instead of multiplying probabilities. The log turns products into sums: log⁡(ab)=log⁡a+log⁡b\log(ab) = \log a + \log b. The log of a probability is a log-probability.

In this book, log⁡\log means the natural log, base ee. Log-probabilities are never positive: log⁡1=0\log 1 = 0 for a certain event, and rarer events go further below 0. For our toppings:

log⁡(0.644, 0.237, 0.087, 0.032)=(−0.440, −1.440, −2.440, −3.440)\log(0.644,\ 0.237,\ 0.087,\ 0.032) = (-0.440,\ -1.440,\ -2.440,\ -3.440)

In the table's last column, each order adds one of these four numbers. So the sum goes down in steady steps, about 1.44 per order on average, and ends at −288.04-288.04. That's an everyday number.

Predict first

Model A gives the 200 orders a total log-probability of −288. Model B gives −300. Which model gave the orders the higher chance?

The log-probability −288.04-288.04 still says everything we need. To compare two models, compare their log-probabilities. The probability itself is e−288.04≈8×10−126e^{-288.04} \approx 8 \times 10^{-126}, but we never need to compute it.

That's why ML works with log-probabilities almost everywhere. Training a model usually means making the log-probability of the real data as big as possible.

Look at the log-probabilities again

The log-probabilities (−0.440, −1.440, −2.440, −3.440)(-0.440,\ -1.440,\ -2.440,\ -3.440) are the scores (2,1,0,−1)(2, 1, 0, -1) minus 2.440. Here's why. Take the log of softmax:

log⁡softmax(z)i=log⁡ezi∑jezj=zi−log⁡∑jezj\log \mathrm{softmax}(\mathbf{z})_i = \log \frac{e^{z_i}}{\sum_j e^{z_j}} = z_i - \log \sum_j e^{z_j}

The log undoes the exp on top, and division becomes subtraction. The part being subtracted is the same for every ii. It has a name: logsumexp, the log of the sum of the exponentials:

logsumexp(z)=log⁡∑jezj\mathrm{logsumexp}(\mathbf{z}) = \log \sum_j e^{z_j}

For our scores, logsumexp(2,1,0,−1)=log⁡11.475=2.440\mathrm{logsumexp}(2, 1, 0, -1) = \log 11.475 = 2.440. It's a little bigger than the biggest score, 2. That's typical: logsumexp is a smooth version of max.

So log-probabilities straight from scores are log⁡softmax(z)=z−logsumexp(z)\log \mathrm{softmax}(\mathbf{z}) = \mathbf{z} - \mathrm{logsumexp}(\mathbf{z}). This is called log-softmax.

In the figure, the pale bars are the scores and the solid bars are the log-probabilities. Every solid bar is its pale bar moved down by the same amount, logsumexp. Raise the pepperoni score and watch logsumexp: once one score is well ahead, logsumexp sits a hair above it.

pepp.marg.veggiehawai.−10−505score / log-probabilitylogsumexp 2.44

Pale bars: scores. Solid bars: log-probabilities = score − 2.44.

logsumexp = 2.44 · biggest score = 2.00 · gap 0.44

Log-probabilities: pepperoni −0.44, margherita −1.44, veggie −2.44, hawaiian −3.44

Log-probabilities are the scores shifted down by logsumexp. logsumexp is always a little above the biggest score, and closer when one score stands out: a smooth max.

Big scores: subtract the biggest first

Scores in real models can be large. Try scores of (100, 101, 102)(100,\ 101,\ 102). Exponentials of big scores are enormous: e100≈2.7×1043e^{100} \approx 2.7 \times 10^{43}. Subtracting the biggest score first keeps every number at most 1.

Adding the same number to every score changes nothing. So subtract the biggest score, mm, first:

logsumexp(z)=m+log⁡∑jezj−m\mathrm{logsumexp}(\mathbf{z}) = m + \log \sum_j e^{z_j - m}

Why is this allowed? Pull eme^m out of every term: ∑jezj=em∑jezj−m\sum_j e^{z_j} = e^m \sum_j e^{z_j - m}. Then the log turns that product into the sum m+log⁡∑jezj−mm + \log \sum_j e^{z_j - m}.

Now the biggest exponential is e0=1e^0 = 1, and every other one is between 0 and 1. The numbers never get large, whatever the scores.

Worked example

logsumexp(100, 101, 102), with small numbers only

  1. The biggest score is m=102m = 102. Subtract it: (−2, −1, 0)(-2,\ -1,\ 0).

Again logsumexp, 102.408, sits a little above the biggest score, 102. 2.6 shows why this matters on a computer.

Common misconceptions

"argmax and max are the same thing." Max is the biggest value, 2. Argmax is where it is, position 0 (pepperoni).

"A score of 0 means probability 0." No. A score of 0 gives e0=1e^0 = 1 before dividing, so veggie gets 0.087. Only the gaps between scores matter.

"A probability of 0.644 means the model is right 64% of the time." Only if the model has been trained to make that true. Softmax always gives numbers that add up to 1, whether or not they match reality. The cross-entropy in the next lesson measures how well they match.

"Higher temperature makes the model smarter." Temperature doesn't change the ranking, only how spread out the probabilities are.

Recap

  • Argmax gives the position of the biggest score. It's all or nothing.
  • Softmax turns scores into probabilities: exponentiate, then divide by the total. Only the gaps between scores matter.
  • Temperature divides the scores first. Low temperature approaches argmax; high temperature approaches an even split.
  • Products of many probabilities get tiny, so we add log-probabilities instead.
  • log⁡softmax(z)=z−logsumexp(z)\log\mathrm{softmax}(\mathbf{z}) = \mathbf{z} - \mathrm{logsumexp}(\mathbf{z}). Logsumexp is a smooth max, and subtracting the biggest score first keeps every exponential at most 1.

Section 2 shows how to compute these in PyTorch.