Softmax in 2 lines: why subtracting the max prevents overflow

cneuralnetwork · x · 2026-10-29

A concise explainer on the numerically stable softmax trick: e^x overflows to inf/NaN for large logits, but subtracting a constant from every logit leaves softmax unchanged since the e^(-c) factor cancels. Choosing c = max(x) makes the largest exponent e^0 = 1 and all others ≤ 1. Includes a two-line PyTorch implementation.

Original post →

More from Research

Research channel →