NLP: Softmax with temperature

Turn a model's next-token scores into probabilities at different temperatures.

What to build

A program that applies softmax with temperature to a list of logits.

Requirements

  • For each temperature T, compute p = softmax(logit ÷ T) over the tokens, subtracting the largest scaled logit before exponentiating so that very large logits do not overflow.
  • Print T=t: token p token p ... with t as written in the input and each probability to three decimal places, tokens in input order.

Input

Line 1: the tokens. Line 2: their logits. Line 3: one or more temperatures.

Output

One line per temperature.

Constraints

At most 1,000 tokens; temperatures are positive.

Example

Input:

yes no maybe
2 1 0
1 2

Output:

T=1: yes 0.665 no 0.245 maybe 0.090
T=2: yes 0.506 no 0.307 maybe 0.186

Why it works

Softmax makes positive numbers that add up to 1 while keeping the order of the logits; dividing by T scales the gaps between logits, so a small T sharpens the distribution and a large T flattens it, and subtracting the maximum changes nothing but keeps the exponentials small.

Examples

Example 1 · Input
yes no maybe
2 1 0
1 2
Expected output
T=1: yes 0.665 no 0.245 maybe 0.090
T=2: yes 0.506 no 0.307 maybe 0.186
Example 2 · Input
mat floor sofa moon
3 2 1.5 0
1 0.5
Expected output
T=1: mat 0.609 floor 0.224 sofa 0.136 moon 0.030
T=0.5: mat 0.842 floor 0.114 sofa 0.042 moon 0.002

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

Hints

Hint 1

Divide every logit by T, subtract the largest result, apply `math.exp`, then divide by the sum.

Hint 2

Print T exactly as it was written in the input.

Solution

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

tokens = input().split()
logits = [float(x) for x in input().split()]
for t in input().split():
    scaled = [z / float(t) for z in logits]
    top = max(scaled)
    e = [math.exp(z - top) for z in scaled]
    total = sum(e)
    print(f"T={t}: " + " ".join(f"{tok} {v / total:.3f}" for tok, v in zip(tokens, e)))

Lesson: Attention, transformers and LLMs in practice →