Introduction
How do you know whether a model that writes text is any good? For a classifier the answer is easy: count the correct labels. For generated text there may be many good answers, such as a summary that is worded differently from yours but says the same thing. Evaluation is therefore a set of tools, each with strengths and blind spots. This lesson builds the most common ones from scratch: exact match, n-gram overlap, perplexity, and human or model judges.
How it works
Exact match counts an answer as right only if it equals the reference after cleaning (lower case, no punctuation, no extra spaces). It suits short factual answers, such as "Paris", and fails on anything with several correct wordings.
N-gram overlap compares runs of n words. For a generated text (the candidate) and a reference:
- Precision is the share of the candidate's n-grams that appear in the reference.
- Recall is the share of the reference's n-grams that the candidate contains.
- F1 combines them: 2 × P × R / (P + R).
Matches are clipped: a reference n-gram can be matched only as many times as it occurs there, so the candidate "the the the the" cannot score well by repeating one word. BLEU, a classic score for machine translation, multiplies the geometric mean of the n-gram precisions by a brevity penalty that is 1 for long enough outputs and exp(1 − r/c) when the candidate has c words and the reference r words and c is smaller. Without it, the short candidate "the cat" would get perfect precision. ROUGE, common for summaries, is built from recall instead.
Perplexity (from the next-token lesson) measures how surprised a model is by real text: it is exp of the average negative log probability of the true tokens. Lower is better. It needs no reference answers, but it only compares models that use the same tokenizer and text, and it says nothing about whether answers are true or helpful.
Human judgement is the most trusted standard: people rate answers for correctness, helpfulness and safety, or choose the better of two answers. It is slow and costly. So teams often use a judge model that scores answers against a written rubric. This is cheap and fast, but a judge can prefer longer answers or its own style, so its scores must be checked against human ratings, for example by counting how often the two agree.
Example
import math
reference = "the cat sat on the mat"
candidates = [
"the cat sat on the mat",
"a cat was sitting on the mat",
"the the the the the the",
"the cat",
]
def normalize(text):
return " ".join(w for w in text.lower().split() if w not in ("a", "an", "the"))
def matches(cand, ref, n):
"""Clipped n-gram matches: each n-gram of the reference can be matched only as often as it appears there."""
left = {}
for i in range(len(ref) - n + 1):
gram = " ".join(ref[i : i + n])
left[gram] = left.get(gram, 0) + 1
found = 0
for i in range(len(cand) - n + 1):
gram = " ".join(cand[i : i + n])
if left.get(gram, 0) > 0:
left[gram] -= 1
found += 1
return found
ref = reference.split()
for cand in candidates:
words = cand.split()
exact = normalize(cand) == normalize(reference)
m1 = matches(words, ref, 1)
p1 = m1 / len(words)
p2 = matches(words, ref, 2) / (len(words) - 1)
r1 = m1 / len(ref)
bp = 1.0 if len(words) >= len(ref) else round(math.exp(1 - len(ref) / len(words)), 12)
bleu = bp * round(math.sqrt(p1 * p2), 12)
print(f"{cand!r}: exact {exact}, P1 {p1:.2f}, P2 {p2:.2f}, R1 {r1:.2f}, BP {bp:.2f}, BLEU-2 {bleu:.2f}")
# Perplexity: the model's probabilities for the true words of one sentence
probs = [0.5, 0.25, 0.5, 0.125]
perplexity = round(math.exp(round(-sum(math.log(p) for p in probs) / len(probs), 12)), 12)
print(f"perplexity {perplexity:.3f}")
# A simulated judge scores each answer 1 to 5; two judges are compared
judge_a = [5, 4, 2, 1]
judge_b = [5, 3, 2, 2]
agree = sum(1 for a, b in zip(judge_a, judge_b) if a == b)
print(f"judges agree on {agree} of {len(judge_a)} answers")Line by line
normalizelower-cases the text and removes the words a, an and the, a simple cleaning for exact match.matchescounts clipped n-gram matches: it counts the reference n-grams first, and every match uses one up.- For each candidate the loop prints exact match, unigram precision P1, bigram precision P2, unigram recall R1, the brevity penalty BP and a two-gram BLEU score.
- The word-for-word copy scores 1 everywhere. The repeated word "the" matches only twice, because the reference has two of them. The short "the cat" has perfect precision but low recall, and the brevity penalty pulls its BLEU down.
- The perplexity of four true-word probabilities is the exp of their average negative log, here 3.364 (the geometric mean of the reciprocals).
- The two judges agree on 2 of 4 answers, which is the kind of check used to decide whether a judge model can be trusted.
Expected result
'the cat sat on the mat': exact True, P1 1.00, P2 1.00, R1 1.00, BP 1.00, BLEU-2 1.00
'a cat was sitting on the mat': exact False, P1 0.57, P2 0.33, R1 0.67, BP 1.00, BLEU-2 0.44
'the the the the the the': exact False, P1 0.33, P2 0.00, R1 0.33, BP 1.00, BLEU-2 0.00
'the cat': exact False, P1 1.00, P2 1.00, R1 0.33, BP 0.14, BLEU-2 0.14
perplexity 3.364
judges agree on 2 of 4 answers
In the real world
Translation systems report BLEU, summarizers report ROUGE, and question-answering systems report exact match. Teams building assistants keep a fixed test set, run every new model on it and compare the scores, then read a sample of answers by hand. Leaderboards also ask people to pick the better of two anonymous answers. Judge models speed this up, but the best teams still audit a sample of judgements with people.
Common mistakes
- Trusting overlap scores as quality: a fluent paraphrase can score low and a copied sentence can score high.
- Forgetting clipping, so repeated words inflate precision.
- Ignoring length, so very short outputs look precise.
- Comparing perplexity across different tokenizers.
- Believing a judge model without checking it against human ratings.
Best practices
- Use more than one measure, and read a sample of real outputs yourself.
- Keep one fixed test set that is never used for tuning prompts or training.
- Write the judging rubric down, and measure how well the judge agrees with people.
Summary
Generated text has many good answers, so evaluation combines several tools. Exact match suits short answers. N-gram precision, recall and F1, with clipping and a brevity penalty, give BLEU and ROUGE style scores. Perplexity measures how well a model predicts real text, and human or judge-model ratings capture meaning but are slower and must be checked. No single measure is enough.
Quick check
Key takeaways
- There is often more than one good answer, so no single number tells the whole story.
- Overlap scores are cheap and repeatable but reward matching words, not meaning.
- Use several measures, a fixed test set, and sampled human checks; a judge model must itself be checked against people.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.