Stable Softmax

Softmax turns logits into probabilities:

softmax_i = exp(logit_i) / sum(exp(logits))

The direct version can overflow when logits are large. The stable version subtracts the maximum logit first:

shifted = logits - max(logits)
softmax_i = exp(shifted_i) / sum(exp(shifted))
logitssubtract maxexpnormalizesame probabilities, safer numbers
Stable softmax shifts logits before exponentiation.

This does not change the probabilities. Adding or subtracting the same constant from all logits leaves softmax unchanged.

For logits [1000, 1001], subtract 1001:

[-1, 0]

Now exponentials are safe enough to compute.

DL-C16-T05-001Exercise: Stable shift

For logits [2, 8, 4], what maximum should be subtracted?

Compute it first, then check your number.

DL-C16-T05-002Exercise: Shifted logits

Logits are [2, 8]. After subtracting the maximum, what is the first shifted logit?

Compute it first, then check your number.