Loading...
Loading...
You are given a list of doubles x.
Let z be the list of the same length where z[i] = exp(x[i]) and exp is the exponential function. Let s be the sum of the entries of z. The softmax of x is the list y where y[i] = z[i] / s.
Return the softmax of x. Every entry of the result is positive and the entries sum to 1.
Note that entries of x can be large, so exp(x[i]) and s can be far outside the range of a double. An answer is accepted when each of its entries differs from the corresponding expected entry by at most 10^-5 absolute or relative error.
Input: x = [1]
Output: [1.0]
Explanation: There is only one entry, so it receives all of the mass and the result is `[1.0]`.
Input: x = [0,0,0]
Output: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
Explanation: All three entries are equal, so each one gets an equal share of `1/3`.
Input: x = [1,2,3]
Output: [0.09003057317038046,0.24472847105479764,0.6652409557748218]
Explanation: Each entry is `exp(x[i])` divided by `exp(1) + exp(2) + exp(3)`.
x.length ≤103x[i] ≤109