Interactive Explainer
Softmax & Cross-Entropy
Classification networks output raw scores. Softmax turns them into a probability distribution; cross-entropy scores how surprised the model is by the true answer; and — the beautiful part — the gradient that trains the network turns out to be exactly predicted minus target, $p - y$. Slide the logits below and watch all three move together.
Three quantities, one picture
For a $K$-class problem the network emits logits $z_1,\dots,z_K$. Softmax with temperature $T$ maps them to probabilities, cross-entropy reads off the loss for a one-hot target $y$, and the gradient with respect to the logits is startlingly clean:
That last identity is why softmax + cross-entropy is the default pairing in deep learning: no messy chain rule survives, just “how wrong is each class, signed.” The widget below is built entirely around these three formulas.
Logits in, loss and gradient out
Three classes. Set the logits, choose which class is actually correct, and optionally sharpen or flatten with temperature. The left panel is the softmax distribution (the true class outlined); the right panel is the gradient $p - y$, the signed force each logit feels during backprop.
Why the gradient is just $p - y$
Cross-entropy looks like it should have an ugly derivative — there is a $\log$, and a $\sum$ inside the softmax. But the $\log$ and the $\exp$ cancel almost perfectly. For the true class $y$:
- For a non-true class, $y_k = 0$, so the gradient is $+p_k$ — push that logit down in proportion to how much probability it wrongly claimed.
- For the true class, $y_k = 1$, so the gradient is $p_{\text{true}} - 1 \le 0$ — push that logit up, hardest when $p_{\text{true}}$ is small.
- The gradients sum to zero: probability that leaves the wrong classes flows into the right one. Softmax conserves it.
CrossEntropyLoss takes raw logits, not
probabilities). Applying softmax yourself and then a separate cross-entropy double-counts and gives
wrong, unstable gradients. Feed logits; let the loss do the softmax.