Temperature Sampling
Implement temperature-scaled sampling from logits.
Temperature controls the randomness of predictions:
- Divide logits by temperature T
- Apply softmax: pi​=∑j​ezj​/Tezi​/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:
1.0 2.0 1.0 0.1
[0.6590, 0.2424, 0.0986]
Temperature = 1.0 (standard softmax): Logits / T = [2.0, 1.0, 0.1] Subtract max: [0.0, -1.0, -1.9] exp: [1.0, 0.3679, 0.1496] Sum = 1.5175 Probabilities: [1.0/1.5175, 0.3679/1.5175, 0.1496/1.5175] = [0.6590, 0.2424, 0.0986]
Constraints:
- T > 0
- Use softmax with numerical stability (subtract max)
- Round each probability to 4 decimal places
Background Knowledge
The problem involves temperature-scaled sampling, a technique used in natural language processing (NLP) and machine learning to control the randomness of predictions. The core concept here is the softmax function, which is used to normalize the input values (logits) into a probability distribution. The temperature parameter, denoted as T, plays a crucial role in shaping this distribution. A higher temperature leads to a more uniform distribution, making the predictions more random, while a lower temperature results in a more peaked distribution, making the predictions more deterministic.
The softmax function is defined as pi​=∑j​ezj​ezi​​, where zi​ represents the input logits. In the context of temperature-scaled sampling, the logits are divided by the temperature T before applying the softmax function, resulting in pi​=∑j​ezj​/Tezi​/T​. This modification allows for the control of the randomness in the predictions. Understanding the properties of the softmax function and how the temperature affects the output distribution is essential for tackling this problem.
In NLP applications, temperature-scaled sampling is often used in text generation tasks, such as language modeling or machine translation. By adjusting the temperature, the model can produce more diverse or more focused outputs, depending on the specific requirements of the task. The ability to control the randomness of predictions makes temperature-scaled sampling a valuable tool in many NLP applications.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.