Log-Softmax

EasyMachine LearningMathArray

Description

Given an array of logits, return the log-softmax: each element minus the log-sum-exp of the array. Use the max-subtraction trick for numerical stability. Round each result to 4 decimal places.

Examples

Input:[1,2,3]
Output:[-2.4076,-1.4076,-0.4076]
Explanation:

Each logit has the log-sum-exp of all logits subtracted from it, giving the logarithm of the softmax probabilities.

Input:[0,0]
Output:[-0.6931,-0.6931]
Explanation:

Each logit has the log-sum-exp of all logits subtracted from it, giving the logarithm of the softmax probabilities.

Input:[5]
Output:[0]
Explanation:

Each logit has the log-sum-exp of all logits subtracted from it, giving the logarithm of the softmax probabilities.

Constraints

  • 1 ≤ length ≤ 10³

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.