Deep learning: A stable softmax

Turn raw scores into probabilities without overflowing.

What to build

A program that computes softmax probabilities from logits.

Requirements

  • Compute pᵢ = e^(zᵢ − max) / Σⱼ e^(zⱼ − max).
  • Print the probabilities with four decimal places, separated by spaces.

Input

One line: the logits.

Output

One line.

Constraints

At most 100 logits; each between −10,000 and 10,000.

Example

Input:

1 2 3

Output:

0.0900 0.2447 0.6652

Why it works

Subtracting the same number from every logit multiplies the top and bottom of each fraction by the same factor, so the result is unchanged but the exponentials never overflow.

Examples

Example 1 · Input
1 2 3
Expected output
0.0900 0.2447 0.6652
Example 2 · Input
0 0
Expected output
0.5000 0.5000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Subtract the largest logit from every logit before calling math.exp.

Hint 2

Divide each exponential by their sum.

Solution

Show one correct solution (try it yourself first)
import math

zs = list(map(float, input().split()))
top = max(zs)
exps = [math.exp(z - top) for z in zs]
total = sum(exps)
print(" ".join(f"{e / total:.4f}" for e in exps))

Lesson: Softmax and multi-class classification →