Temperature Sampling
Text Generation & NLP Evaluation DS practice problem on Onlearn.
Difficulty: medium.
Topics: Temperature Sampling, Temperature Scaling Factor, Categorical Distribution Sampling, Logit Sharpening, Top-k Filtering, Softmax Normalization, Natural Language Processing, Probabilistic Modeling, Information Theory, Deep Learning Architectures, Statistical Inference, Autoregressive Language Modeling, Softmax Probability Distributions, Stochastic Decoding Strategies, Logit Transformation Techniques, Entropy-based Uncertainty Estimation.
In language model text generation, temperature sampling is a technique used to control the randomness of predictions by scaling the logits (raw model output scores) before converting them to probabilities. Write a Python function temperature sampling(logits, temperature) that takes: logits: A 1D numpy array of raw scores (can be positive or negative) temperature: A float controlling the sharpness of the output distribution The function should return a Python list of probabilities representing the temperature scaled probability distribution over the vocabulary. Key behaviors to implement: A temperature of 1.0 should produce the standard softmax distribution Low temperatures (close to 0) should produce sharper, more confident distributions High temperatures should produce softer, more uniform distributions When temperature is 0 or negative, return a one hot distribution placing all probability mass on the token with the highest logit (first occurrence in case of ties) Ensure numerical stability in your softmax computation to handle large logit values without overflow.