The entropy part is straightforward but the numerically stable piece is where it gets real.
Start by explaining the mathematical definition of entropy for a softmax distribution, then derive a numerically stable computation using the log-sum-exp trick. Emphasize the importance of avoiding overflow/underflow and provide a step-by-step algorithm with code-level details.
Pro tip: Mention that you can compute entropy as log(sum(exp(logits))) - sum(softmax(logits) * logits) to avoid explicitly computing probabilities, which is more stable and efficient.
State that entropy H(p) = -sum(p_i * log(p_i)) where p = softmax(logits). Explain that softmax(logits)_i = exp(logits_i) / sum(exp(logits_j)).
Point out that directly computing exp(logits) can overflow if logits are large, and log(p_i) can be -inf if p_i underflows to zero. This leads to NaN or incorrect results.
Subtract the maximum logit M from all logits before exponentiation: p_i = exp(logits_i - M) / sum(exp(logits_j - M)). This ensures the largest exponent is 0, preventing overflow.
Use the formula H = log(sum(exp(logits - M))) + M - sum(softmax(logits) * logits). Alternatively, compute log_probs = logits - M - log(sum(exp(logits - M))) and then H = -sum(exp(log_probs) * log_probs).
Discuss handling of -inf logits (e.g., masked positions) by excluding them or setting probability to 0. Verify stability with extreme values and compare against naive implementation.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.