Sweep the sampling temperature

LabEasy20 min15 checks

samplingtemperatureentropytop-p

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().

  1. 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.
  2. entropy_bits(probs) returns H = -\sum_i p_i \log_2 p_i in bits. A zero probability adds 0.
  3. sweep_temperature(logits, temperatures) returns two float arrays of the length of temperatures: the entropy and the top 1 probability \max_i p_i at each temperature.
  4. temperature_for_entropy(logits, target, lo=0.05, hi=10.0, iters=60) finds the temperature where the entropy equals target, by bisection. Do iters rounds. In each round, let m be the midpoint of lo and hi. If the entropy at m is below target, move lo to m. Else move hi to m. After the last round, return the midpoint of lo and hi.
  5. 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 to p. If no prefix reaches p because of rounding, it returns the number of tokens.

Then do the experiment in answers().

  1. Set the temperature to 1.0. Report the entropy as entropy_t1.
  2. Set the temperature to 0.7. Report the top 1 probability as top1_t07.
  3. Change the temperature over TEMPERATURES. Measure the entropy at each one. Report the first grid temperature with an entropy greater than or equal to TARGET_ENTROPY as first_grid_temperature.
  4. Find the exact crossing with temperature_for_entropy(LOGITS, TARGET_ENTROPY). Report it as crossing_temperature.
  5. Apply top p with TOP_P at the temperatures 0.7, 1.0 and 1.5. Report the kept counts as kept_p09_t07, kept_p09_t10 and kept_p09_t15.

Input and output

NameTypeShapeMeaning
logitsfloat array(V,)one score for each token
temperaturefloatscalarthe divisor T of the logits, T > 0
probsfloat array(V,)a distribution that sums to 1
temperatureslist of floats(k,)the temperatures of the sweep
targetfloatscalarthe entropy to reach, in bits
pfloatscalarthe top p mass, 0 < p <= 1
  • softmax_t returns a float array of shape (V,).
  • entropy_bits returns a float.
  • sweep_temperature returns a tuple of two float arrays of shape (k,).
  • temperature_for_entropy returns a float. kept_count returns an int.
  • answers() returns a dict with the seven keys above. Each value is a Python float.

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-6 from 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.5
pythonsweep_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)
text2
Provided 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 hints

    Solution and notes

    Locked

    Pass 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 --examples

    Start my_answer.py from starter.py.

    Your code

    Python loads on the first run

    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.