Concept · Chapter 2: The Math Toolkit
Softmax
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
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 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
Exponentiate each score
, ,
Add them up
Divide each by the total
cat , dog , car . They sum to 1 (up to rounding).
A one-point gap in score (cat vs dog) became a ratio of in probability. That's the "exponentially more weight" in the one-sentence summary.
The equation
Deep diveWhy computers subtract the maximum firstShould know
overflows a floating-point number. Because only differences between scores matter, implementations compute , 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 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
3Blue1Brown
Transformers, the tech behind LLMs | Deep Learning Chapter 5
A visual tour of a GPT from input text to next-token probabilities — ideal before or right after Chapter 7.