softmax()
Apply the softmax function to a list of logits.
Usage
softmax(logits)Converts a vector of raw scores (logits) into a probability distribution where each element is in (0, 1) and all elements sum to 1.
Parameters
logits: list-
A list of numeric values (raw scores).
Returns
list-
A list of probabilities summing to 1.0.
Notes
Applies the softmax function:
\[ \sigma(z)_i = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}} \]
For numerical stability, the implementation subtracts the maximum logit value before exponentiation:
\[ \sigma(z)_i = \frac{e^{z_i - \max(z)}}{\sum_{j=1}^{K} e^{z_j - \max(z)}} \]
This prevents overflow when logit values are large.
Examples
>>> result = softmax([1.0, 2.0, 3.0])
>>> round(sum(result), 5)
1.0>>> softmax([0.0, 0.0])
[0.5, 0.5]