Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Sampling, temperature and top-k

After this lesson: I can choose the next token with greedy decoding, temperature and top-k.

55 min lesson1 commandJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Why the same question gets a different answer

Sampling is how a language model picks its next token from its probabilities. Temperature and top-k are the settings that shape that choice. They are why the same prompt can give different text each time.

The model's numbers do not change between two answers. Only the choice does.

By the end of this lesson

Note

I can choose the next token with greedy decoding, temperature and top-k.

Key words

WordWhat it meansExample
Greedy decodingalways taking the most likely token
Samplingdrawing a token at random by its probability
Temperaturea number the scores are divided by before softmax0.5
Top-kkeeping only the k most likely tokensk = 2
Seeda start number that makes random draws repeat
Promptthe text the model continues

The cells students fill in are left blank in this preview.

Greedy decoding always takes the top token

After "the bus is", our invented model gives scores for four next words. Softmax turns them into probabilities.

Greedy decoding always takes the most likely token, here late. It gives the same text every time. Over a long text it often gets stuck repeating itself, because the likeliest next step can lead back to the same place.

Worked example: scores to probabilities

Next wordScoree to the scoreProbability
late320.090.644
full27.390.237
here12.720.087
blue01.000.032

The cells students fill in are left blank in this preview.

Temperature sharpens or flattens the choice

Sampling draws a token at random, each with its probability. So late comes out about 64 times in 100, and blue about 3.

Temperature changes those chances first. Divide every score by it, then apply softmax. Below 1, the top token gets even more likely. Above 1, the chances even out and rarer words appear more often.

Worked example: three temperatures

Next wordTemperature 0.5Temperature 1Temperature 2
late0.8650.6440.455
full0.1170.2370.276
here0.0160.0870.167
blue0.0020.0320.102

The cells students fill in are left blank in this preview.

A thousand draws at two temperatures

Command
docker exec jupyter python3 -c "import numpy as np; s = np.array([3, 2, 1, 0]); print([np.bincount(np.random.default_rng(7).choice(4, size=1000, p=np.exp(s / t) / np.exp(s / t).sum()), minlength=4).tolist() for t in (1.0, 0.5)])"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Top-k and the seed

Top-k keeps only the k most likely tokens and shares their chances out again so they add up to 1. With k = 2, here and blue can never appear.

A random draw starts from a seed. The same seed gives the same draws, and a new seed gives new ones. That is why one prompt can give different text, and why a fixed seed makes a test repeat.

Worked example: top 2 at temperature 1

Next wordProbabilityKept?Shared out again
late0.644yes0.644 / 0.881 = 0.731
full0.237yes0.237 / 0.881 = 0.269
here0.087no0
blue0.032no0

The cells students fill in are left blank in this preview.

Sample with a temperature and a seed

Code: python
import math
import random

words = ["late", "full", "here", "blue"]
scores = [3, 2, 1, 0]      # the model's scores after "the bus is"
temperature = 1.0
seed = 1

powers = []
for s in scores:
    powers.append(math.exp(s / temperature))
total = sum(powers)
probs = []
for p in powers:
    probs.append(p / total)
print("probabilities:", [round(p, 3) for p in probs])

random.seed(seed)
draws = random.choices(words, weights=probs, k=8)
print("eight draws:", draws)
# Change seed to 2 and press Run: different draws, same probabilities.
# Then set temperature to 0.3. What happens to the draws?

A common mistake

Warning

Many people think a model gives a new answer because it changed its mind or learnt from them. Its numbers are the same. The difference comes from the random draw, so a fixed seed and the same settings give the same text.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.