Softmax: from logits to probabilities

A classifier ends with a vector of logits z, one per class, and softmax turns them into a probability distribution p. Drag any logit up or down, then slide every logit at once, and change the temperature. The matrix on the right is how each pi responds to each zj.

Logits, their softmax probabilities, and the softmax Jacobian Left: one bar per class for its logit, draggable. Middle: the probability softmax assigns to each class. Right: the Jacobian matrix of partial derivatives of each probability with respect to each logit, positive entries orange and negative blue, with each row's sum beside it.
classes
add k to every logit
temperature T

1. Only the differences between logits matter

Softmax exponentiates each logit and divides by the total, so the result is positive and sums to 1:

Slide add k and every bar on the left moves, while the probabilities do not move at all. Work out why from the formula: what happens to each ezi+k, and to their sum? The consequence is that a network’s n logits carry only n−1 numbers’ worth of information. With two classes, p1 depends only on z1 − z2, and it is the familiar sigmoid of that difference, the single output unit of squared error vs. cross-entropy.

2. The Jacobian

Each probability depends on every logit. The matrix of partial derivatives ∂pi/∂zj on the right has a pattern worth finding for yourself before reading on: look at the signs on and off the diagonal, the sum of each row, and what happens to the whole matrix when one class takes almost all the probability.

Raising zi raises pi (the diagonal is positive) and takes probability from every other class (the off-diagonal is negative). Every row sums to zero, because the probabilities always sum to 1: whatever one class gains, the others lose. The diagonal pi(1 − pi) is largest at pi = ½ and vanishes as pi → 0 or 1, so when one class dominates, the whole Jacobian goes to zero: softmax saturates, just as a sigmoid does. That is why the loss that follows it matters; cross-entropy’s logarithm cancels exactly this flatness, and squared error does not.

3. Temperature

Dividing the logits by a temperature T before softmax sharpens or flattens the distribution without changing its order:

As T → 0 the largest logit takes everything, and softmax becomes argmax as a one-hot vector; as T → ∞ every class gets 1/n. T is how language models trade predictability for variety when they sample, and how knowledge distillation softens a teacher’s outputs so that a student can learn from the wrong answers’ relative probabilities. Note the 1/T in front of the Jacobian: a cold softmax is nearly flat almost everywhere and very steep near a tie.

4. Computing it without overflow

Section 1 is also how softmax is computed safely. e1000 overflows a floating-point number, so a direct implementation turns large logits into ∞/∞ = NaN. Since adding a constant changes nothing, subtract the largest logit first; then every exponent is at most 0, and the answer is exact. Press What if k = 1000? to see both. The same trick, log∑ezj = m + log∑ezj−m, is the stable log-sum-exp inside every cross-entropy loss.