Subtract the row maximum, exponentiate nonpositive shifts, and divide by their shared sum.
Problem trace
Subtracting a shared maximum changes exponentials without changing ideal probabilities
Read it this way:Logits (1000,1001) shift to (-1,0). Their softmax probabilities are about 0.269 and 0.731 without exponentiating 1000. Does adding another thousand to both original logits change their shifted difference or their probability shares?
Sources and provenance
Full interview answer 11 min read
Convert finite class scores into probabilities while avoiding overflow from large but valid score values.
Problem
Input. A floating-point NumPy array logits and a valid normalization axis, defaulting to the last axis.
Output. Return softmax probabilities with the same shape, summing to one along each selected-axis slice.
Rules. Probabilities are proportional to exponentiated scores within each slice.
Require finite inputs, a nonempty selected axis, and representable score differences.
Preserve the input. Large common score offsets must not cause exponential overflow.
Example. Scores [1000, 1001, 999] return approximately [0.244728, 0.665241, 0.090031].
Scores [0, 0] return [0.5, 0.5], not raw exponentials [1, 1].
Picture
Subtract the row maximum 1001: [−1,0,−2].
Exponentials are approximately [0.367879,1,0.135335], with sum 1.503215.
Divide by that shared sum to obtain [0.244728,0.665241,0.090031].
Problem trace
Separate three-logit row: subtract its maximum, exponentiate, normalize
Input and goalConvert logits to probabilities without numeric overflow.
Find the maximum logitFor logits [1000,1001,999], the row maximum is 1001. Direct exp(1001) can overflow.
10000max = 1001100119992
max
1
index 0
1000
index 1
1001
Shift without changing ratiosSubtract 1001 from every entry to get [-1,0,-2]. Pairwise gaps are unchanged, and every shifted logit is at most zero.
-10largest = 001-22
arithmetic[1000,1001,999] - 1001
arithmetic
not shown→[1000,1001,999] - 1001
index 0
1000→-1
index 1
1001→0
Exponentiate safelyExponentials are [exp(-1),exp(0),exp(-2)] = [0.3679,1.0000,0.1353], whose sum is 1.5032.
0.36790exp(0)1.000010.13532
denominator0.3679 + 1.0000 + 0.1353 = 1.5032
exp(0)
not shown→1
largest
1→not shown
denominator
not shown→0.3679 + 1.0000 + 0.1353 = 1.5032
Normalize the rowDivide each exponential by 1.5032 to get [0.2447,0.6652,0.0900]. The rounded probabilities sum to 0.9999 (approximately 1).
Use it when exponentials normalize scores into probabilities and large positive logits may overflow even though only relative differences should affect the result.
Keep true
Subtracting one constant from every row element leaves every exponential ratio unchanged; after max shifting, the largest exponent is exp(0)=1 and none can overflow.
Reuse it
Before exponentiating normalized scores, exploit shift invariance to center at the maximum; preserve the reduced dimension so broadcasting back across the row is explicit.
For logits [1000,1001,999], the row maximum is 1001. Direct exp(1001) can overflow. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why does subtracting 1001 change the exponentials but not the ideal softmax probabilities?
Reference drawing
Problem trace
Subtracting a shared maximum changes exponentials without changing ideal probabilities
Execution trace
Input and goalConvert logits to probabilities without numeric overflow.
Find the maximum logitFor logits [1000,1001,999], the row maximum is 1001. Direct exp(1001) can overflow.
10000max = 1001100119992
max
1
index 0
1000
index 1
1001
Shift without changing ratiosSubtract 1001 from every entry to get [-1,0,-2]. Pairwise gaps are unchanged, and every shifted logit is at most zero.
-10largest = 001-22
arithmetic[1000,1001,999] - 1001
arithmetic
not shown→[1000,1001,999] - 1001
index 0
1000→-1
index 1
1001→0
Exponentiate safelyExponentials are [exp(-1),exp(0),exp(-2)] = [0.3679,1.0000,0.1353], whose sum is 1.5032.
0.36790exp(0)1.000010.13532
denominator0.3679 + 1.0000 + 0.1353 = 1.5032
exp(0)
not shown→1
largest
1→not shown
denominator
not shown→0.3679 + 1.0000 + 0.1353 = 1.5032
Normalize the rowDivide each exponential by 1.5032 to get [0.2447,0.6652,0.0900]. The rounded probabilities sum to 0.9999 (approximately 1).
Use it when exponentials normalize scores into probabilities and large positive logits may overflow even though only relative differences should affect the result.
Keep true
Subtracting one constant from every row element leaves every exponential ratio unchanged; after max shifting, the largest exponent is exp(0)=1 and none can overflow.
Reuse it
Before exponentiating normalized scores, exploit shift invariance to center at the maximum; preserve the reduced dimension so broadcasting back across the row is explicit.
For logits [1000,1001,999], the row maximum is 1001. Direct exp(1001) can overflow. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why does subtracting 1001 change the exponentials but not the ideal softmax probabilities?
Read it this way:Logits (1000,1001) shift to (-1,0). Their softmax probabilities are about 0.269 and 0.731 without exponentiating 1000. Does adding another thousand to both original logits change their shifted difference or their probability shares?
Code
A common shift cancels from normalized ratios:
∑jezj−mezi−m=∑jezjezi.
Taking m=maxjzj makes every exponent argument nonpositive, with at least one exponential equal to one.
Retain dimensions for row-wise broadcasting.
Check:[0,−1000] gives log probabilities [0,−1000] in float64, although e−1000 underflows.
Use when: losses or sequence scores consume log probabilities.
Check the axis and dtype
The wrong axis normalizes the wrong groups.
Integer subtraction can overflow before exponentiation. Max shifting does not repair NaNs or a completely masked row.
Try subtracting the mean
Why does mean subtraction preserve exact ratios but offer less overflow protection?
Check your answer
Any common shift cancels algebraically.
After mean subtraction, the largest exponent argument can remain positive.
Maximum subtraction guarantees nonpositive arguments for valid finite arithmetic.
Numerical limits
The four-decimal trace probabilities sum to 0.9999 because of rounding.
Test the full-precision result with a tolerance, not exact equality to rounded values.
For a batch, verify every intended row sums to one and compare a column-normalized case separately.
Very large input differences can overflow subtraction even when both original values are finite.
Low precision can erase small differences before this function receives the logits.
Underflow of very small exponentials can still produce zero probabilities.
The log variant avoids first forming those probabilities, but does not remove finite-range arithmetic limits.
Production libraries may fuse softmax or log-softmax with downstream operations.
The NumPy temporary arrays establish no fused-kernel memory or speed claim.