Machine learning building blocks
Log-softmax
ml.log-softmaxAlso called log softmax, logsoftmax, log_softmax, log probabilities
Log of the softmax probabilities, computed as each score minus the log-sum-exp of all scores. Taking the log of a computed softmax loses accuracy and fails where the softmax overflows or underflows.
Notation
logits (unnormalized scores)
x ∈ ℝn
number of entries
Size, at least 2
Result: the log-probabilities
vector
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ (-0.69314718056, -0.69314718056)
Given
Result
≈ (-2.40760596444, -1.40760596444, -0.407605964444)
Given
Result
≈ (-0.69314718056, -0.69314718056)
Common mistakes
Takes the log of a naively computed softmax
exp overflows for scores above about 709, and very negative log-probabilities underflow to log(0) = −inf.
Fix: Compute xi − LSE(x) with the maximum subtracted inside the log-sum-exp, or call torch.log_softmax / scipy.special.log_softmax.
Source
Goodfellow et al. 2016, §6.2.2.3
Goodfellow, Ian; Bengio, Yoshua; Courville, Aaron. Deep Learning. MIT Press, 2016.