The naive approach of exponentiating the logits directly just produces inf or 0 for large inputs and you get garbage.
Derive the entropy formula in terms of logits using the log-sum-exp trick to avoid overflow, then implement it with a numerically stable algorithm. Explain the derivation and discuss trade-offs like time complexity and precision.
Pro tip: Mention that you can compute entropy as logsumexp(logits) - sum(softmax(logits) * logits) and that using a max-shift ensures stability even for extreme values. Also note that for very large logits, the entropy approaches 0, so handle edge cases.
Start from the definition H = -sum(p_i * log p_i) and substitute p_i = exp(z_i)/Z, where Z = sum(exp(z_j)). Simplify to H = log Z - sum(exp(z_i) * z_i)/Z.
Compute log Z stably as m + log(sum(exp(z_i - m))), where m = max(z_i). This prevents overflow when logits are large.
Calculate sum(exp(z_i - m) * z_i) / sum(exp(z_i - m)) to get the expected logit, avoiding underflow/overflow.
Compute H = log Z - expected_logit. For extreme logits, ensure that if one logit dominates, entropy is near zero; handle empty or single-element lists.
Discuss O(n) time and O(1) extra space, and compare with naive softmax which may overflow. Mention precision considerations for float32 vs float64.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by explaining the numerically stable entropy formula using the log-sum-exp trick, then design a streaming version that maintains a running maximum and a running sum of exponentials. When a new logit exceeds the current maximum, rescale the accumulated sum by multiplying it with exp(old_max - new_max) to keep the computation stable. Finally, compute entropy as log(sum_exp) + max - sum(logit * exp(logit - max)) / sum_exp, ensuring O(1) memory.
Pro tip: Emphasize that the rescaling step is crucial for numerical stability and that it only occurs when the maximum changes, so the amortized cost is O(1) per logit. Also, mention that you can avoid storing all logits by maintaining only the running max and the sum of exponentials.
Restate the goal: compute entropy of a stream of logits with O(1) memory and numerical stability. Confirm that logits arrive one at a time and we only need the final entropy.
Write the entropy formula using the log-sum-exp trick: H = log(sum(exp(logit - max))) + max - sum(logit * exp(logit - max)) / sum(exp(logit - max)). Explain why subtracting the max prevents overflow.
Maintain three variables: running maximum (max), sum of exponentials (sum_exp), and sum of logit * exp(logit - max) (sum_logit_exp). Update these incrementally as each logit arrives.
When a new logit exceeds the current max, update max and rescale sum_exp and sum_logit_exp by multiplying with exp(old_max - new_max). This keeps the accumulated sums consistent with the new max.
After processing all logits, compute entropy using the maintained sums. State that time per logit is O(1) amortized (rescaling only on max change) and memory is O(1).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.