A model's raw scores become probabilities through softmax. Compute it, safely.
What to build
A program that turns logits into probabilities with a numerically stable softmax.
Requirements
- Print the probabilities in the same order, each with 3 decimal places.
- It must work for very large logits, such as 1000.
Input
One line of logits.
Output
One line.
Constraints
1 to 1000 logits, each between −10⁴ and 10⁴.
Example
Input:
0 0
Output:
0.500 0.500
Why it works
Subtracting the maximum keeps every exponent at most 0, so e^z never overflows, and the shift cancels out when dividing by the sum.
Examples
Example 1 · Input
0 0
Expected output
0.500 0.500
Example 2 · Input
1 2 3
Expected output
0.090 0.245 0.665
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Subtract the largest logit from every logit first.
Hint 2
Exponentiate each, then divide by their sum.
Solution
Show one correct solution (try it yourself first)
import math
logits = list(map(float, input().split()))
top = max(logits)
exps = [math.exp(z - top) for z in logits]
total = sum(exps)
print(*[f"{e / total:.3f}" for e in exps])