All problems
Softmax of Vector
Problem
Compute the softmax of a vector:
softmax(v)[i] = exp(v[i] - m) / sum_j exp(v[j] - m)
with m = max(v) for numerical stability. The outputs are positive and sum to 1.
Hint
Softmax is invariant to adding a constant to every input, which is why max subtraction is safe.
Test cases
case 1
in: [[0,0]]
expected: [0.5,0.5]
case 2
in: [[1,2,3]]
expected: [0.09003057317038046,0.24472847105479764,0.6652409557748218]
case 3
in: [[0,1]]
expected: [0.2689414213699951,0.7310585786300049]
case 4
in: [[1000,1000]]
expected: [0.5,0.5]
Starter codePython
def softmax(v):
# Your code here
pass4 test cases · run your code against them in the editor