Multiclass softmax
Quick answer
Multiclass logistic regression assigns probability estimates across mutually exclusive classes. Softmax converts one score per class into a normalized distribution; cross-entropy measures the probability placed on the observed class. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Normalize three scores
from math import expz=[1000,1001,1002]m=max(z)a=[exp(v-m) for v in z]print([round(v/sum(a),6) for v in a])Expected output:
[0.090031, 0.244728, 0.665241]All three exponentials are safe; the highest score receives the largest probability.
How it works
For K mutually exclusive classes, score z_k = b_k + w_k·x. Softmax gives p_k = exp(z_k)/sum_j exp(z_j). Subtract the maximum score m from every score before exponentiating: multiplying every numerator and denominator by exp(-m) preserves the ratio. This also ensures all shifted scores are at most zero. Equal scores give equal probabilities. The largest score and the largest softmax probability have the same index, with ties requiring an explicit policy. For a single observed class t, loss = -ln(p_t), equivalently logsumexp(z)-z_t. A stable equivalent is (m-z_t)+ln(sum(exp(z_j-m))). Its derivative with respect to score z_k is p_k-1[k=t]. For independent simultaneous labels, this single-distribution assumption is wrong; independent sigmoid outputs may be appropriate instead. A binary model has one score in scikit-learn, while a multinomial model has a score per class. Probability columns follow the fitted classes_ order; do not assume labels are consecutive integers. In a matrix of scores, normalize across the class axis separately for every sample.
Check multinomial probabilities
from sklearn.linear_model import LogisticRegressionimport numpy as npfrom scipy.special import softmaxm=LogisticRegression(solver="lbfgs", l1_ratio=0.0).fit([[-3],[-2],[0],[1],[3],[4]],["a","a","b","b","c","c"])z=m.decision_function([[0],[3]])p=m.predict_proba([[0],[3]])print(m.classes_.tolist())print(bool(np.allclose(softmax(z,axis=1),p)))print(p.sum(axis=1).round(6).tolist())Expected output:
['a', 'b', 'c']True[1.0, 1.0]The matrix has one column per class; each row is normalized independently.
Common mistakes
Do not normalize across the entire batch. Avoid exponentiating raw large scores, and never use a single softmax distribution for independent labels without a task-specific reason.
Practice and next steps
Compute the probabilities for scores [7,7,7]. Explain what changes when 1000 is added to every score. Complete the knowledge check and coding exercise. Next: Regularized logistic regression.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Multiclass softmax — knowledge check
Apply multiclass softmax to new examples.
Which axis is normalized for scores shaped (samples, classes)?
What is softmax([4,4,4])?
What does subtracting a common maximum do in exact arithmetic?
What is the loss for observed class t?
Which task may require separate sigmoid outputs instead of softmax?