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
| Word | What it means | Example |
|---|---|---|
| Greedy decoding | always taking the most likely token | |
| Sampling | drawing a token at random by its probability | |
| Temperature | a number the scores are divided by before softmax | 0.5 |
| Top-k | keeping only the k most likely tokens | k = 2 |
| Seed | a start number that makes random draws repeat | |
| Prompt | the 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 word | Score | e to the score | Probability |
|---|---|---|---|
| late | 3 | 20.09 | 0.644 |
| full | 2 | 7.39 | 0.237 |
| here | 1 | 2.72 | 0.087 |
| blue | 0 | 1.00 | 0.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 word | Temperature 0.5 | Temperature 1 | Temperature 2 |
|---|---|---|---|
| late | 0.865 | 0.644 | 0.455 |
| full | 0.117 | 0.237 | 0.276 |
| here | 0.016 | 0.087 | 0.167 |
| blue | 0.002 | 0.032 | 0.102 |
The cells students fill in are left blank in this preview.
A thousand draws at two temperatures
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 word | Probability | Kept? | Shared out again |
|---|---|---|---|
| late | 0.644 | yes | 0.644 / 0.881 = 0.731 |
| full | 0.237 | yes | 0.237 / 0.881 = 0.269 |
| here | 0.087 | no | 0 |
| blue | 0.032 | no | 0 |
The cells students fill in are left blank in this preview.
Sample with a temperature and a seed
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.