Log-Sum-Exp
A rearrangement that computes the log of a sum of exponentials without ever overflowing, by factoring out the largest term first.
The trick rests on one identity: where is the largest . Subtracting the maximum makes the biggest exponent exactly , so nothing can overflow, and adding back afterwards makes the result exact rather than approximate.
Without it, softmax and cross-entropy break on ordinary inputs. A logit of 800 gives , which is inf in any float type — float32 overflows at about 88 — and inf/inf is nan, which then contaminates every gradient it touches. In the other direction a small probability underflows to zero and log(0) is -inf.
This is why frameworks fuse softmax and cross-entropy into a single operation and expect raw logits rather than probabilities. log_softmax(x) is not log(softmax(x)) in floating point, and hand-assembling the second one is a real and common bug. Use the fused API.