Apply a temperature

Temperature reshapes a model's distribution before sampling. Compute the probabilities at a given temperature.

What to build

A program that applies a temperature to logits and prints the resulting probabilities.

Requirements

  • Probabilities are softmax(logit / T).
  • Print them in the same order with 3 decimal places.

Input

Line 1: the temperature T. Line 2: the logits.

Output

One line.

Constraints

0.01 ≤ T ≤ 100; 1 to 1000 logits.

Example

Input:

1
0 0

Output:

0.500 0.500

Why it works

Dividing by T scales the differences between logits: below 1 the differences grow and the top token takes more probability; above 1 they shrink and the probabilities move closer together.

Examples

Example 1 · Input
1
0 0
Expected output
0.500 0.500
Example 2 · Input
0.5
1 0
Expected output
0.881 0.119

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

Hints

Hint 1

Divide every logit by the temperature, then apply a stable softmax.

Hint 2

Subtract the largest scaled logit before exponentiating.

Solution

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

t = float(input())
scaled = [z / t for z in map(float, input().split())]
top = max(scaled)
exps = [math.exp(s - top) for s in scaled]
print(*[f"{e / sum(exps):.3f}" for e in exps])

Lesson: Decoding: temperature, top-k, top-p and beam search →