Skip to content
Road to Intelligence

Concept · Chapter 2: The Math Toolkit

Softmax

Must knowImplement20 minDifficulty

Softmax turns any list of scores into a probability distribution — positive numbers that sum to 1 — giving exponentially more weight to larger scores.

The problem

Models naturally produce raw scores (any real numbers, possibly negative), but we need probabilities or weights that are positive and sum to 1.

The solution

Exponentiate every score (making it positive, and amplifying differences) and divide by the total.

The consequence

It becomes the standard last step of classifiers, the way attention scores become weights, and the source of every next-token distribution an LLM samples from.

You should understand first

  1. Probability and Distributions
  2. Softmax

Intuition

A model scores three candidate next words: cat 2.0, dog 1.0, car −1.0. Those numbers are useful for ranking but they aren't probabilities: one is negative and they don't sum to 1. Softmax is the conversion: make everything positive, keep the ranking, and normalize.

Why exponentiate rather than just shift and divide? Because eze^z is always positive, it grows fast (so a clearly better score wins clearly), and it pairs naturally with logarithms in the cross-entropy loss used to train almost every classifier and language model.

Tiny numeric example

  1. Exponentiate each score

    e2.0≈7.39e^{2.0} \approx 7.39,   e1.0≈2.72\;e^{1.0} \approx 2.72,   e−1.0≈0.37\;e^{-1.0} \approx 0.37

  2. Add them up

    7.39+2.72+0.37=10.487.39 + 2.72 + 0.37 = 10.48

  3. Divide each by the total

    cat =7.39/10.48≈0.71= 7.39/10.48 \approx 0.71,   \; dog ≈0.26\approx 0.26,   \; car ≈0.04\approx 0.04. They sum to 1 (up to rounding).

A one-point gap in score (cat vs dog) became a ratio of e1≈2.7e^1 \approx 2.7 in probability. That's the "exponentially more weight" in the one-sentence summary.

The equation

softmax⁡(z)i=ezi/T∑jezj/T\operatorname{softmax}(\mathbf{z})_i = \frac{e^{z_i / T}}{\sum_{j} e^{z_j / T}}
Deep diveWhy computers subtract the maximum firstShould know

e1000e^{1000} overflows a floating-point number. Because only differences between scores matter, implementations compute softmax⁡(z−max⁡(z))\operatorname{softmax}(\mathbf{z} - \max(\mathbf{z})), which gives the identical result with every exponent ≤ 0. You'll see this "max trick" in every real implementation — including the one running the interactives on this site.

Where it appears in AI

  • Classification: the last layer of a classifier outputs one score (a logit) per class; softmax turns them into class probabilities.
  • Attention: each token's similarity scores with every other token go through softmax to become attention weights.
  • Language models: the model's logits over its whole vocabulary become the next-token distribution. Sampling with a temperature is exactly the TT in the equation.

What to remember

  • softmax(z)ᵢ = exp(zᵢ) / Σⱼ exp(zⱼ).
  • Outputs are positive and sum to 1; the order of scores is preserved.
  • Only differences between scores matter — adding a constant to all of them changes nothing.
  • Dividing scores by a temperature T > 1 flattens the distribution; T < 1 sharpens it.

Watch