Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Numerical stability

Softmax that never overflows

Easy · Target 8 minutes · Python and NumPy

Given a list of real numbers x (logits), return the list of softmax probabilities p, where pi = exi / Σj exj. Your function must return finite, correct values for any finite input, including logits in the thousands.

Examples

Input x = [1, 2, 3]

Output [0.09, 0.2447, 0.6652]

Each step of 1 in the logit multiplies the probability by e.

Input x = [1000, 1001, 1002]

Output [0.09, 0.2447, 0.6652]

The same answer: adding 999 to every logit changes nothing.

Input x = [-10000, 0, 10000]

Output [0, 0, 1]

The first two are below the smallest float64. Zero is the correct answer.

Constraints

  • 1 ≤ len(x) ≤ 10⁴
  • −10⁵ ≤ x[i] ≤ 10⁵
  • Answers are checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Try np.exp(1000.0). Then ask what you could do to the logits that leaves the answer unchanged.

Hint 2

Adding the same constant c to every logit multiplies numerator and denominator by eᶜ. Which c guarantees no exponent is positive?

Hint 3

Subtract max(x). The largest term becomes e⁰ = 1, so the sum is at least 1 and nothing overflows or divides by zero.

Where candidates go wrong

  • Subtracting the mean. With x = [0, 2000] the mean is 1000 and e¹⁰⁰⁰ still overflows.
  • Clipping the logits to a range such as [−500, 500]. It avoids the overflow by changing the answer.
  • Returning a NumPy array of dtype int when the input is a list of integers: convert to float first.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

This is the opening question in a large share of ML coding screens, because it separates candidates who have used softmax from candidates who understand it. The naive version passes every small test and fails on the first realistic one.

The theory behind it is in the lesson Numerically stable softmax and log-sum-exp.


Keep going