Stable Softmax and Log-Sum-Exp
Factor out the largest logit to retain the same probabilities while avoiding overflow in exponentiation.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Common shifts cancel after normalization
For logits \(z_i\), subtract \(m=\max_i z_i\) before exponentiating. Multiplying all exponential terms by \(e^{-m}\) cancels in the ratio, and the largest adjusted exponential is one. For logits \(1000,1001\), evaluate exponential terms at \(-1,0\) and obtain probabilities about \(0.269,0.731\). The true probabilities do not require representing \(e^{1001}\).
Compute log probabilities before tiny values round away
The logarithm of the denominator is \(m+\log\sum_i e^{z_i-m}\). Subtracting this expression from logits gives log probabilities without taking a logarithm of a rounded zero. If a mask removes all positions, the application needs an explicit result policy. Log loss uses this computation for reliable scoring. Attention normalizes each query row using the same operation.[1]