Temperature Sampling
Implement temperature scaling for language model logits.
Temperature controls the randomness of sampling by scaling logits before softmax: P(i)=∑j​ezj​/Tezi​/T​
Higher T → more uniform (creative), lower T → more peaked (deterministic).
Input:
- Line 1: temperature T
- Line 2: space-separated logits
Output: Probability distribution after temperature scaling, rounded to 4 decimal places.
Example:
1.0 2.0 1.0 0.1
0.6590 0.2424 0.0986
- The temperature T is given as 1.0, which means the logits will be scaled by this value.
- The logits zi​ are given as 2.0, 1.0, and 0.1, and we calculate the scaled logits: zi​/T=2.0/1.0=2.0, 1.0/1.0=1.0, and 0.1/1.0=0.1.
- We then apply the softmax function: P(i)=∑j​ezj​/Tezi​/T​=e2.0+e1.0+e0.1e2.0​, P(i)=e2.0+e1.0+e0.1e1.0​, and P(i)=e2.0+e1.0+e0.1e0.1​.
- Calculating these values gives us the probabilities: P(1)≈0.6590, P(2)≈0.2424, and P(3)≈0.0986.
Constraints:
- T > 0
- Use numerically stable softmax (subtract max)
- Round to 4 decimal places
More from LLM 3: Applications & Evaluation
Background Knowledge
The problem involves implementing temperature scaling for language model logits, which is a technique used to control the randomness of sampling in language models. The softmax function is used to normalize the logits, which are the unnormalized scores output by the model, into a probability distribution. The temperature scaling formula is given by P(i)=∑j​ezj​/Tezi​/T​, where zi​ is the logit score, T is the temperature, and P(i) is the probability of the ith outcome.
The temperature T is a hyperparameter that controls the level of randomness in the sampling process. A higher temperature leads to a more uniform distribution, which means that the model is more likely to sample from a wider range of possible outcomes. On the other hand, a lower temperature leads to a more peaked distribution, which means that the model is more likely to sample from a smaller set of possible outcomes. This is because the softmax function with a lower temperature will assign higher probabilities to the outcomes with higher logit scores, making them more likely to be sampled.
The concept of temperature scaling is related to the idea of entropy in information theory, which measures the amount of uncertainty or randomness in a probability distribution. By adjusting the temperature, we can control the level of entropy in the output distribution, which can be useful in various applications such as language modeling, text generation, and decision-making under uncertainty.
Algorithm/Approach
The general approach to solving this problem involves implementing the temperature scaling formula using the given input values. This can be achieved by following a simple algorithm pattern: (1) read the input values, (2) apply the temperature scaling formula to the logits, and (3) output the resulting probability distribution.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Read the temperature T from the input
- Read the space-separated logits from the input
- Apply the temperature scaling formula to each logit score
- Calculate the softmax of the scaled logit scores to obtain the probability distribution
- Round the output probabilities to 4 decimal places
Common Pitfalls
Some common pitfalls to watch out for when implementing this solution include:
- Incorrectly applying the temperature scaling formula
- Failing to normalize the output probabilities using the softmax function
- Not rounding the output probabilities to the correct number of decimal places
- Not handling edge cases, such as division by zero or very large input values
Time & Space Complexity
The time complexity of this solution is expected to be O(n), where n is the number of logit scores, since we need to iterate over each logit score to apply the temperature scaling formula. The space complexity is also O(n), since we need to store the output probability distribution, which has the same number of elements as the input logit scores.