v9.1.1
Menu

Temperature Sampling

mediumPROText Generation & AdvancedNLP 3: Advanced

Implement temperature-scaled sampling from logits.

Temperature controls the randomness of predictions:

  1. Divide logits by temperature T
  2. Apply softmax: pi=ezi/T∑jezj/Tp_i = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}}

Higher T -> more uniform (random), lower T -> more peaked (deterministic).

Since we need deterministic output for testing, instead of sampling, return the probability distribution after temperature scaling.

Input format:

  • Line 1: Temperature T (float)
  • Line 2: Space-separated logits (floats)

Output: The probability distribution after temperature scaling, as a list of floats rounded to 4 decimal places.

Example

Input
1.0
2.0 1.0 0.1
Output
[0.6590, 0.2424, 0.0986]
Reasoning
  • T is a positive temperature and logits are raw scores z=(2.0,1.0,0.1)z = (2.0, 1.0, 0.1), one per token. The output is one probability per token, in the same order, summing to 1.
  • With T=1.0T = 1.0 the scaled scores are unchanged: zi/T=(2.0,1.0,0.1)z_i/T = (2.0, 1.0, 0.1). Shifting every score down by the largest, m=2.0m = 2.0, gives (0,−1.0,−1.9)(0, -1.0, -1.9). The ratios survive because the common factor e−me^{-m} cancels between numerator and denominator.
  • Exponentiating: e0=1e^{0} = 1, e−1≈0.36788e^{-1} \approx 0.36788, e−1.9≈0.14957e^{-1.9} \approx 0.14957, with total ≈1.51745\approx 1.51745.
  • Dividing each by the total: 1/1.51745≈0.659001/1.51745 \approx 0.65900, 0.36788/1.51745≈0.242430.36788/1.51745 \approx 0.24243, 0.14957/1.51745≈0.098570.14957/1.51745 \approx 0.09857, which gives [0.6590, 0.2424, 0.0986] rounded to 4 decimal places.
  • Notice how TT reshapes this. At T=0.5T = 0.5 the gaps double to (0,−2,−3.8)(0, -2, -3.8) and the top token rises to 0.86380.8638; at T=2.0T = 2.0 the gaps halve and it falls to 0.50170.5017. The ranking never changes, only how sharp it is.

Constraints

  • T > 0
  • Use softmax with numerical stability (subtract max)
  • Round each probability to 4 decimal places

Learn this in

More from NLP 3: Advanced

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.