Sub topic 4
Multiclass outputs with softmax and cross-entropy
This chapter assumes you know binary logistic regression, the sigmoid function, and basic NumPy. It covers how a model turns K scores into one probability distribution over K classes, and how cross-entropy loss trains it.
From one score to K scores
A binary model produces one score and passes it through a sigmoid. A multiclass model produces one score per class. These raw scores are called logits. They can be any real number and do not sum to anything in particular.
Softmax converts a vector of logits into probabilities:
p_k = exp(z_k) / sum_j exp(z_j)Every output is positive and the outputs sum to 1. Because the classes share one denominator, raising one class's probability lowers the others. The classes compete, which matches the multiclass assumption that exactly one class is correct.
import numpy as np
def softmax(z):
z = z - z.max() # subtract the max for numerical stability
e = np.exp(z)
return e / e.sum()
logits = np.array([2.0, 1.0, -1.0])
p = softmax(logits)
print(p.round(3), p.sum().round(3))The output is about [0.705 0.259 0.035] with a sum of 1.0. Subtracting the maximum does not change the result, because the shift cancels in the ratio. It prevents exp from overflowing on large logits.
Categorical cross-entropy
The loss for one example is the negative log of the probability given to the true class:
loss = -log(p_true)With a one-hot target y, this is the sum -sum_k y_k * log(p_k). Every term except the true class is multiplied by zero. A confident correct prediction (p_true near 1) gives a loss near 0. A confident wrong prediction gives a very large loss, because -log(p) grows without bound as p approaches 0.
import numpy as np
def cross_entropy(probs, true_index):
return -np.log(probs[true_index])
probs = np.array([0.705, 0.259, 0.035])
for true_index in range(3):
print("true class", true_index, "loss", round(cross_entropy(probs, true_index), 3))The losses are about 0.35, 1.35 and 3.35. The model is penalized most when the true class is the one it considered least likely.
A useful gradient
Combined with softmax, the gradient of the loss with respect to each logit is simply p_k - y_k. The error signal is the predicted probability minus the target. This is the same form you saw for binary logistic regression. It is why frameworks fuse the two steps into one function, such as CrossEntropyLoss in PyTorch, which takes raw logits and class indices. Applying softmax yourself before such a loss is a common error. It applies softmax twice and flattens the gradients.
Common errors
- Using softmax for multilabel data. If several classes can be true at once, softmax forces them to share probability mass. Use independent sigmoids instead, as in the next chapter.
- Overflow from a naive softmax. Always subtract the maximum logit, or use a library function.
- Reading probabilities as calibrated. Softmax outputs sum to 1, but they are often overconfident. Calibration is covered later.
- Passing probabilities to a loss that expects logits. Check the documentation of your framework.
Exercise
A three-class model outputs logits [0.0, 0.0, 0.0] for an example whose true class is 1. What are the softmax probabilities, and what is the cross-entropy loss?
Show answer
Equal logits give a uniform distribution, so each probability is 1/3. The loss is -log(1/3) = log(3), about 1.099.