Numerically Stable Softmax
Problem statement
Given a nonempty array of finite logits, return its softmax probabilities in the same order.
Compute the mathematically equivalent maximum-shifted form so large logits do not overflow.
Function
softmax(logits: double[]) → double[]Examples
Example 1
logits = [1.0,2.0,3.0]return = [0.09003057317038046,0.24472847105479764,0.6652409557748218]Subtracting 3 keeps all exponential arguments nonpositive.
Constraints
1 <= logits.length <= 200.-1000 <= logits[i] <= 1000.- Results use absolute tolerance
1e-12.