Score observed tokens
For a sequence with scored positions, add the negative log probabilities of the observed tokens and divide by
Training can feed the actual preceding tokens at every position. This is called teacher forcing. Prompts, padding and other masked positions can be excluded from the loss by omitting them from both numerator and denominator. A corpus-level mean divides the total loss by the total number of scored tokens. An unweighted average of per-document means answers a different question when document lengths differ.
If the assigned probabilities were and the total negative log-likelihood is the mean is and perplexity is
Compare perplexities on the same data, tokenization and scored positions. Using different tokenizations changes the unit being averaged. A model that assigns all observed tokens probability has perplexity which motivates the name. Perplexity is a probability score and offers no direct guarantee about the factual accuracy of generated text.
Generate a continuation
Each generated token becomes part of the next prefix. With logits and temperature the sampling probabilities are proportional to The odds of token against token are A lower temperature increases the odds in favour of the higher logit. Greedy decoding selects a largest logit at each step. Top- sampling retains candidates selected by the largest scores, with a tie rule if needed, then renormalises the selected probabilities. Changing the sampling rule changes the generation distribution. Evaluate the original model’s perplexity with the original probabilities.
For two logits separated by halve the temperature and compare the resulting odds.
from math import exp, log
gap = log(3)
for temperature in (1.0, 0.5):
print(f"{exp(gap / temperature):.0f}")3
9Exercises
Find perplexity if the model assigns probability to each of the three observed tokens at their respective prefixes.
Show solution✓ Checked
The mean loss is so perplexity is
One document has mean loss over three tokens. Another has mean loss over one token. Find the corpus-level mean.
Show solution✓ Checked
The sum is Divide by four to obtain
At temperature the odds of against are Find the odds at temperature with the same logits.
Show solution✓ Checked
Doubling the temperature halves the log odds. The new odds are