15 — Core Math

Softmax & Probabilities#

ClassificationMathematical
◆ The PatternConverting raw scores into a probability distribution

Softmax maps a vector of real-valued logits to probabilities that sum to 1. It amplifies the largest logit, making the winner more decisive.

σ(zᵢ) = e^zᵢ / Σⱼ e^zⱼ
zᵢ = logit for class i  |  numerator = exponentiated score  |  denominator = normalisation
// Adjust logits — watch probabilities redistribute
Temperature scaling: σ(z/T). T<1 → sharper (more confident). T>1 → softer (more uniform). Used in knowledge distillation and language model sampling.
Pattern bridge: Converting logits to probabilities that sum to 1 is a probability distribution in action. Temperature scaling changes the "confidence" — hot = uniform = uncertain market, cold = peaked = consensus. Sampling from these probabilities, with temperature, top-k or top-p, is the subject of decoding strategies.
Worked example

Scores of 2, 1 and 0.1 become probabilities 0.659, 0.242, 0.099. Halving the temperature sharpens them to 0.864, 0.117, 0.019; doubling it flattens them to 0.502, 0.304, 0.194. Scores of 1000 and 1001 overflow a naive implementation (nan); subtracting the largest score first gives the correct 0.269, 0.731.

Where it misleads
  • Softmax outputs look like probabilities but are often over-confident; temperature scaling on held-out data calibrates them (Guo et al. 2017).
  • Only differences between scores matter; adding a constant to all of them changes nothing.
  • A high softmax probability is not evidence that the input is like the training data.
In code
def softmax(z, T=1.0):
    z = np.asarray(z, float) / T
    e = np.exp(z - z.max())                        # subtract the max: same result, no overflow
    return e / e.sum()

p = softmax([2.0, 1.0, 0.1])
cold, hot = softmax([2.0, 1.0, 0.1], T=0.5), softmax([2.0, 1.0, 0.1], T=2.0)
with np.errstate(over='ignore', invalid='ignore'):
    naive = np.exp([1000.0, 1001.0]) / np.exp([1000.0, 1001.0]).sum()   # overflows
stable = softmax([1000.0, 1001.0])
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Each snippet carries its own example numbers; the comments say which are made up or simulated.

Sources
  • Deep Learning, I. Goodfellow, Y. Bengio & A. Courville, MIT Press, 2016 — chapter 6
  • C. Guo, G. Pleiss, Y. Sun & K. Q. Weinberger, “On Calibration of Modern Neural Networks”, ICML, 2017

Last reviewed October 2026 · How this site checks its content · Report a mistake

Check yourself

1. How do softmax([2, 1, 0]) and softmax([102, 101, 100]) compare?

Show answer

C. Adding a constant to every input cancels out. That is why implementations subtract the maximum first, for numerical stability.

2. What happens to softmax as the temperature approaches 0?

Show answer

C. Dividing the inputs by a small temperature magnifies their differences; a large temperature flattens them.

3. A network’s softmax gives 0.9 for class A. Is it right 90% of the time on such cases?

Show answer

C. Softmax outputs sum to 1, but nothing forces them to match observed frequencies. Temperature scaling on validation data is a common fix (Guo et al. 2017).

Linked from 3
← Previous
Gradient Clipping
Open in the full reader, with the topic sidebar