Sweep the sampling temperature
Lesson: GPT — Causal Language ModelingCheat sheetTrack: LLM inferenceTrack: Experiment labs
Problem
A decoder turns the logits of the next token into a distribution and draws from it. Temperature changes how flat that distribution is. Top p then cuts off the tail. In this lab you sweep the temperature on fixed logits and measure the entropy, the top 1 probability and the number of tokens that top p keeps.
support.py gives the 10 token vocabulary and the logits from the GPT lesson demo, LOGITS = [3.2, 1.1, 2.8, 0.4, 0.9, 1.5, -0.2, 2.1, 0.7, 0.1]. It also gives the grid TEMPERATURES from 0.1 to 3.0 in steps of 0.1, TARGET_ENTROPY = 2.0 bits and TOP_P = 0.9.
Task
Write five helper functions and answers().
softmax_t(logits, temperature)returns p_i = e^{z_i / T} / \sum_j e^{z_j / T}. It must not overflow for large logits or a small T.entropy_bits(probs)returns H = -\sum_i p_i \log_2 p_i in bits. A zero probability adds 0.sweep_temperature(logits, temperatures)returns two float arrays of the length oftemperatures: the entropy and the top 1 probability \max_i p_i at each temperature.temperature_for_entropy(logits, target, lo=0.05, hi=10.0, iters=60)finds the temperature where the entropy equalstarget, by bisection. Doitersrounds. In each round, let m be the midpoint ofloandhi. If the entropy at m is belowtarget, moveloto m. Else movehito m. After the last round, return the midpoint ofloandhi.kept_count(probs, p)sorts the probabilities from high to low and returns the length of the shortest prefix whose cumulative sum is greater than or equal top. If no prefix reachespbecause of rounding, it returns the number of tokens.
Then do the experiment in answers().
- Set the temperature to 1.0. Report the entropy as
entropy_t1. - Set the temperature to 0.7. Report the top 1 probability as
top1_t07. - Change the temperature over
TEMPERATURES. Measure the entropy at each one. Report the first grid temperature with an entropy greater than or equal toTARGET_ENTROPYasfirst_grid_temperature. - Find the exact crossing with
temperature_for_entropy(LOGITS, TARGET_ENTROPY). Report it ascrossing_temperature. - Apply top p with
TOP_Pat the temperatures 0.7, 1.0 and 1.5. Report the kept counts askept_p09_t07,kept_p09_t10andkept_p09_t15.
Input and output
| Name | Type | Shape | Meaning |
|---|---|---|---|
logits | float array | (V,) | one score for each token |
temperature | float | scalar | the divisor T of the logits, T > 0 |
probs | float array | (V,) | a distribution that sums to 1 |
temperatures | list of floats | (k,) | the temperatures of the sweep |
target | float | scalar | the entropy to reach, in bits |
p | float | scalar | the top p mass, 0 < p <= 1 |
softmax_treturns a float array of shape(V,).entropy_bitsreturns a float.sweep_temperaturereturns a tuple of two float arrays of shape(k,).temperature_for_entropyreturns a float.kept_countreturns an int.answers()returns a dict with the seven keys above. Each value is a Pythonfloat.
Constraints
2 <= V <= 1000. The logits are finite and can be larger than 700.- Do not change the input arrays.
- Do not round the answers. The checks accept a relative difference of
1e-6from the reference run. - Use numpy and the standard library only.
Examples
pythonsoftmax_t(np.array([1.0, 2.0, 3.0]), 0.5)text[0.01587624 0.11731043 0.86681333]pythonentropy_bits(np.array([0.5, 0.25, 0.25]))text1.5pythonsweep_temperature(LOGITS, [0.5, 1.0, 2.0])text(array([1.50271532, 2.52573217, 3.09650953]), array([0.61269561, 0.38024152, 0.22636197]))pythonkept_count(np.array([0.1, 0.6, 0.3]), 0.8)text2Provided code: support.py
Your code and the checks import this module with from support import .... You do not change it.
import numpy as np
VOCAB = ["the", "cat", "dog", "sat", "ran", "jumped", "on", "mat", "floor", "."]
LOGITS = np.array([3.2, 1.1, 2.8, 0.4, 0.9, 1.5, -0.2, 2.1, 0.7, 0.1])
TEMPERATURES = [round(0.1 * i, 1) for i in range(1, 31)]
TARGET_ENTROPY = 2.0
TOP_P = 0.9
Hints
4 hintsSolution and notes
LockedPass every check with Submit to open the reference solution and the notes.
Run it on your computer
Clone the repository, install numpy and run the same checks in a terminal:
python3 scripts/practice/run.py lab-sampling-temperature my_answer.py
python3 scripts/practice/run.py lab-sampling-temperature my_answer.py --examplesStart my_answer.py from starter.py.
Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.
Keyboard shortcuts
- Ctrl Enter
- Run the examples
- Ctrl Shift Enter
- Submit
- Tab / Shift Tab
- Indent or outdent the lines
- Esc then Tab
- Move the focus out of the editor
- Alt H
- Show the next hint
On a Mac, use Cmd in place of Ctrl.
Select Run to check the examples, or Submit to run every check.