Search

Search papers and lessons

Softmax

Level 2 · 7 min read

Softmax turns a list of arbitrary scores into positive numbers that sum to one, so a network can report a probability for each class.

On this page

  • Colour = topic mix
  • Neural network basics
  • Discrete math
  • Probability
  • Sign in to see your progress

TL;DR

A neural network's last layer often produces raw scores, one per class, that can be any real numbers, negative or large. Softmax converts those scores into probabilities: each output is positive, and all of them add up to 1. The largest score gets the largest probability, but the output is a smooth "soft" version of picking the maximum.

Where you'll see it: as the final layer of image classifiers and language models, where it turns scores over every possible next word into a probability distribution.

Intuition

A vote that is shared out in proportion

Suppose three candidates get scores 2, 1 and 0.1 from a panel. You want to split a single unit of attention among them so that the best-scored candidate gets the most, but nobody gets zero just for being second. Softmax does this in two moves. First it makes every score positive by exponentiating it, so a bigger score becomes exponentially bigger. Then it divides each result by the total, so the pieces sum to one.

The exponential is the important part. The gap between scores of 2 and 1 is only one point, but after exponentiating, e2e^2 is about 2.7 times e1e^1. Scores that are close produce probabilities that are close, and a score that is clearly ahead takes most of the mass. That is why softmax behaves like a smooth version of "take the maximum": it always gives a differentiable answer, and gradients can flow back through it during training.

It is also why the outputs are called "logits" when they come into softmax. A logit is an unnormalised score. With two classes, the difference between the two logits is the log-odds of the first class, and softmax maps logits to probabilities.

flowchart LR
  A[Raw scores z1, z2, z3] --> B[Exponentiate each one]
  B --> C[Divide by the sum]
  C --> D[Probabilities that add to 1]

The math

Definition

Definition: Softmax

For a vector of real scores z=(z1,…,zK)\mathbf{z} = (z_1, \ldots, z_K), the softmax is the vector σ(z)\sigma(\mathbf{z}) whose ii-th entry is

σ(z)i=exp⁡(zi)∑j=1Kexp⁡(zj),i=1,…,K.\sigma(\mathbf{z})_i = \frac{\exp(z_i)}{\sum_{j=1}^{K} \exp(z_j)}, \qquad i = 1, \ldots, K.

Here KK is the number of classes, ziz_i is the score for class ii, and exp⁡\exp is the exponential function. Because exp⁡(z)>0\exp(z) \gt 0 for every real zz, every output is strictly positive. Dividing by the sum makes the outputs add to 1, so they form a probability distribution over the KK classes. The sum in the denominator is the normaliser, and it is what turns raw exponentials into shares.

Two properties that matter

Shift invariance. Adding the same constant cc to every score does not change the output:

σ(z+c1)i=ezi+c∑jezj+c=eceziec∑jezj=σ(z)i.\sigma(\mathbf{z} + c\mathbf{1})_i = \frac{e^{z_i + c}}{\sum_j e^{z_j + c}} = \frac{e^{c} e^{z_i}}{e^{c} \sum_j e^{z_j}} = \sigma(\mathbf{z})_i.

The ece^c factor cancels between numerator and denominator. Only differences between scores matter, not their absolute values.

Derivative. Writing pi=σ(z)ip_i = \sigma(\mathbf{z})_i, the partial derivative of one output with respect to one score is

∂pi∂zj=pi(δij−pj),\frac{\partial p_i}{\partial z_j} = p_i (\delta_{ij} - p_j),

where δij\delta_{ij} is 1 when i=ji = j and 0 otherwise. Raising the score of a class increases its own probability and decreases every other one. This clean form is why softmax pairs so well with the cross-entropy loss: when the true class is one-hot, the gradient of the loss with respect to the scores collapses to pi−yip_i - y_i, the predicted probability minus the target.

Worked examples

Example
Three classes, by hand

Scores z=(2,1,0.1)\mathbf{z} = (2, 1, 0.1).

  1. Exponentiate: e2≈7.389e^{2} \approx 7.389, e1≈2.718e^{1} \approx 2.718, e0.1≈1.105e^{0.1} \approx 1.105.
  2. Sum: 7.389+2.718+1.105=11.2127.389 + 2.718 + 1.105 = 11.212.
  3. Divide each by the sum:
    • p1=7.389/11.212≈0.659p_1 = 7.389 / 11.212 \approx 0.659
    • p2=2.718/11.212≈0.242p_2 = 2.718 / 11.212 \approx 0.242
    • p3=1.105/11.212≈0.099p_3 = 1.105 / 11.212 \approx 0.099
  4. Check: 0.659+0.242+0.099=1.0000.659 + 0.242 + 0.099 = 1.000.

Interpretation: the top score gets about two-thirds of the probability, and the other two share the rest. The score gap of 1 between the first two entries has become a probability ratio of e1≈2.72e^{1} \approx 2.72, which is 0.659/0.242≈2.720.659 / 0.242 \approx 2.72 as expected.

Example
A stability trap with large scores

Scores z=(1000,999)\mathbf{z} = (1000, 999). Computing e1000e^{1000} directly overflows ordinary 64-bit floats, which top out near e709e^{709}.

  1. Use shift invariance: subtract the maximum, z′=(0,−1)\mathbf{z}' = (0, -1). The answer is unchanged.
  2. Exponentiate: e0=1e^{0} = 1, e−1≈0.368e^{-1} \approx 0.368.
  3. Sum: 1+0.368=1.3681 + 0.368 = 1.368.
  4. Divide: p1=1/1.368≈0.731p_1 = 1 / 1.368 \approx 0.731, p2=0.368/1.368≈0.269p_2 = 0.368 / 1.368 \approx 0.269.

Interpretation: subtracting the maximum before exponentiating is the standard trick used in every deep learning library. The largest exponent becomes e0=1e^0 = 1, so nothing can overflow, and the result is exactly the same mathematically.

Tradeoffs and failure modes

Softmax is the right output when classes are mutually exclusive and you want a distribution. It is the wrong choice in several common cases.

SituationProblemBetter choice
Several labels can be true at once (a photo tagged "beach" and "sunset")Softmax forces the probabilities to compete, so one label's gain is another's lossIndependent sigmoids, one per label
Very many classes, such as a 100,000-word vocabularyThe normaliser sums over every class on every step, which is expensiveSampled softmax, or Hierarchical Softmax
Scores are huge and unscaledOutputs saturate near 0 and 1, and gradients vanishNormalise or rescale the inputs; subtract the maximum only to avoid overflow
You need a hard decision, not a distributionSoftmax gives probabilities, not a single winnerTake the argmax at inference time
Calibration matters for the final probabilityModern networks are often overconfident, and softmax faithfully reports that confidenceTemperature scaling after training

A useful knob is the temperature TT: computing softmax(z/T)\mathrm{softmax}(\mathbf{z}/T) with T>1T \gt 1 flattens the distribution, and T<1T \lt 1 sharpens it. Language models expose this directly as a sampling setting.

History

The exponential form is much older than machine learning. The Boltzmann distribution in 19th-century statistical mechanics has the same shape: energies enter an exponential, and dividing by a partition function normalises them. Gibbs made the general case explicit in the early 1900s.

The name and its use as a neural network output come from John Bridle, who around 1990 described normalised exponential outputs for classifier networks and called them a "soft" version of the maximum. The idea caught on because it gave networks a probability output that could be trained with maximum likelihood and differentiated easily. It became standard in the 2010s as deep classifiers grew, and it is now the default final layer for multi-class problems.

Check yourself

QuestionCompute softmax of (0,0,0)(0, 0, 0). What do you get, and why is that the natural answer?Show answer

Each e0=1e^0 = 1, the sum is 3, so every output is 1/31/3. With no information distinguishing the classes, the distribution is uniform.

QuestionFor z=(1,2)\mathbf{z} = (1, 2), compute the softmax. Give the first output to three decimal places.Show answer

e1≈2.718e^1 \approx 2.718 and e2≈7.389e^2 \approx 7.389, with sum 10.10710.107. The first output is 2.718/10.107≈0.2692.718 / 10.107 \approx 0.269, and the second is 0.7310.731.

QuestionWhy does softmax depend only on differences between scores? What does that imply about adding 100 to every logit?Show answer

Adding a constant cc to every score multiplies every exponential by ece^c, and the same factor appears in the sum, so it cancels. Adding 100 to every logit leaves the probabilities unchanged, which is why the subtract-the-maximum trick is safe.

QuestionA spam filter outputs logits for 'spam' and 'not spam' and uses softmax. The logits are (3,−3)(3, -3). Roughly how confident is the model that the message is spam, and what would a larger gap do?Show answer

The difference is 6, so pspam=1/(1+e−6)≈0.9975p_{\text{spam}} = 1 / (1 + e^{-6}) \approx 0.9975. A larger gap pushes the probability closer to 1 quickly, which is why very large logits make a network look certain even when it is wrong.

QuestionYou are building a tagger where an image can have several true tags at once. Should you use softmax at the output? Justify your answer.Show answer

No. Softmax makes the probabilities compete and sum to 1, so adding confidence to one tag removes it from the others. Several true tags need independent probabilities, one per tag, which is what a sigmoid per output gives.

Sources