Classification probabilities
Convert model scores or logits into normalized class probabilities.
SciPy · Function
Computes softmax by exponentiating input values and dividing by the sum of exponentials along a selected axis. The output has the same shape as the input and sums to 1 along that axis.
softmax maps values to an array of the same shape by exponentiating elements and dividing by the sum of exponentials along the chosen axis. With the default axis, the calculation covers the entire input array; specifying an axis applies it along that dimension.
scipy.special.softmax(x, axis=None)| Name | Type | Description |
|---|---|---|
x | array_like | Input array. |
axis | int or tuple of ints, optional | Axis or axes along which to compute softmax. Defaults to None, meaning the entire array. |
ndarray: An array with the same shape as x; values sum to 1 along the specified axis.
import numpy as np
from scipy.special import softmax
scores = np.array([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]])
probabilities = softmax(scores, axis=1)
print(probabilities)
print(probabilities.sum(axis=1))Computes a probability distribution independently for each row and verifies that each row sums to one.
Convert model scores or logits into normalized class probabilities.
Normalize values independently across rows or another selected dimension.
Construct differentiable normalized weights from arbitrary scores.
softmax exponentiates each input element and divides it by the sum of all exponentials in the selected computation scope. For a vector, the result is e^{x_j}/\sum_k e^{x_k} .
Follow-up: What does axis control? It selects the dimension or dimensions along which the normalization is performed; omitting it uses the entire array.
The result has the same shape as the input array. Along the specified axis, its values sum to 1.
Follow-up: How would you normalize every row of a matrix? Pass axis=1 so each row is treated as a separate collection.
The function accepts an array-like x and an optional integer or tuple of integers axis. axis defaults to None, which means computation over the entire array.
Follow-up: What related SciPy function is mathematically connected to softmax? softmax is the gradient of logsumexp.
scipy.special.logsumexp