Sub topic 11
Measuring Quality with Exact Match and F1
This chapter assumes you know basic Python, tokenization, and the idea of a question-answering model that predicts a text span. We will evaluate extractive answers against SQuAD-style reference answers.
Why use two metrics?
An extractive question-answering system returns text copied from a context. Evaluation compares the predicted text with one or more accepted answers.
- Exact match (EM) gives 1 point only when the normalized prediction exactly matches a reference.
- F1 measures token overlap between the prediction and a reference. It gives partial credit when the answer is partly correct.
Both metrics are usually reported on a 0–100 scale. A score of 80 means that the average score is 80 percent.
| Prediction | Reference | Exact match | F1 |
|---|---|---|---|
| `seven million square kilometres` | `about seven million square kilometres` | 0 | 92.3 |
The prediction misses the word “about,” so exact match is zero. Most tokens still overlap, so F1 remains high.
Normalization before scoring
SQuAD-style scoring normalizes text before comparison. Typical normalization:
- Convert letters to lowercase.
- Remove punctuation.
- Remove the English articles
a,an, andthe. - Collapse repeated whitespace.
This prevents harmless formatting differences from lowering the score. For example, The Amazon and amazon can receive the same normalized representation.
Normalization does not understand meaning. The answers car and automobile still have no token overlap unless one is explicitly listed as an accepted reference.
How F1 is calculated
F1 uses token-level precision and recall.
- Precision is the fraction of predicted tokens that also appear in the reference.
- Recall is the fraction of reference tokens that the prediction contains.
- F1 is the harmonic mean of precision and recall.
For a prediction with 3 overlapping tokens out of 4 predicted tokens and 5 reference tokens:
- Precision = 3 / 4 = 0.75
- Recall = 3 / 5 = 0.60
- F1 = 2 × 0.75 × 0.60 / (0.75 + 0.60) ≈ 0.667
Repeated tokens are counted with their available overlap. When a question has several valid reference answers, the evaluator normally uses the best score against any reference.
import evaluate
metric = evaluate.load("squad")
predictions = [
{
"id": "q1",
"prediction_text": "seven million square kilometres",
}
]
references = [
{
"id": "q1",
"answers": {
"text": ["about seven million square kilometres"],
"answer_start": [88],
},
}
]
scores = metric.compute(
predictions=predictions,
references=references,
)
print(scores)
# {'exact_match': ..., 'f1': ...}The id values must match between predictions and references. The answer_start value is needed for the SQuAD data shape, but the metric compares answer text rather than recalculating the position.
Dataset-level scores
The evaluator computes a score for every question and then averages those scores. One very wrong answer can therefore be hidden by many easy examples.
A useful report includes:
- Exact match and F1 for the complete validation set.
- Scores grouped by question type, such as who, when, and why.
- Examples with the largest difference between F1 and exact match.
- Examples where the predicted span is not supported by the context.
A high F1 with a lower exact match often means that the model finds the correct region but includes extra words or omits a short boundary word.
Reading failures correctly
Exact match and F1 do not prove that an answer is factually correct. They measure agreement with the provided references. They also miss some useful distinctions:
- A plausible paraphrase may receive zero overlap.
- A long span containing the correct answer may receive a reasonable F1 but still be poor for users.
- If the context does not contain the answer, a model may confidently select an irrelevant span.
- Incorrect reference boundaries can unfairly lower both metrics.
For production systems, inspect qualitative examples alongside the aggregate scores. Also evaluate whether the selected span is actually supported by the supplied context.
Exercise
A prediction is Paris, France, and the reference is Paris. Which metric is more likely to give partial credit, and why?
Show answer
F1 is more likely to give partial credit because the prediction and reference share the token Paris, even though exact match fails after normalization.