Sub topic 12
BERTScore: greedy matching of token embeddings
Assume you know precision, recall, and F1, and have a rough idea of word embeddings and transformer language models. This chapter focuses on how BERTScore compares a generated sentence with a reference sentence.
Why token overlap is not enough
A generated sentence can express the same idea with different words:
- Reference: “A cat was sitting on the rug.”
- Candidate: “The kitten rested on the carpet.”
Exact n-gram metrics see few matching words. BERTScore can recognize that words such as “cat” and “kitten” may have similar meanings in context.
BERTScore uses a pretrained transformer model to turn each token into a contextual embedding. A contextual embedding is a vector whose value depends on both the token and the surrounding sentence. For example, “bank” receives different vectors in “river bank” and “bank account.”
Cosine similarity between token embeddings
BERTScore compares token vectors with cosine similarity:
\operatorname{cos}(x,y)=\frac{x\cdot y}{\|x\|\|y\|}A value near 1 means that two vectors point in similar directions. For each candidate token, BERTScore finds the reference token with the highest cosine similarity. It then performs the reverse operation for each reference token.
Greedy matching and the three scores
Let the candidate contain tokens \(c1,\ldots,cm\), and the reference contain \(r1,\ldots,rn\). Define \(s(ci,rj)\) as their cosine similarity.
Candidate-to-reference matching produces precision:
P=\frac{1}{m}\sum_{i=1}^{m}\max_j s(c_i,r_j)Reference-to-candidate matching produces recall:
R=\frac{1}{n}\sum_{j=1}^{n}\max_i s(c_i,r_j)BERTScore combines them with the harmonic mean:
F_1=\frac{2PR}{P+R}The matching is called greedy because each token independently chooses its best partner. It is not a one-to-one alignment. Several candidate tokens can select the same reference token.
| Score | Direction | Main question |
|---|---|---|
| Precision | Candidate to reference | How well does generated content match the reference? |
| Recall | Reference to candidate | How much reference content does the candidate cover? |
| F1 | Both directions | What is the balanced similarity? |
import numpy as np
candidate = np.array([[1.0, 0.0], [0.8, 0.6]])
reference = np.array([[0.9, 0.1], [0.0, 1.0]])
# Normalize each token vector, then compute all pairwise cosine similarities.
candidate_unit = candidate / np.linalg.norm(candidate, axis=1, keepdims=True)
reference_unit = reference / np.linalg.norm(reference, axis=1, keepdims=True)
similarities = candidate_unit @ reference_unit.T
precision = similarities.max(axis=1).mean()
recall = similarities.max(axis=0).mean()
f1 = 2 * precision * recall / (precision + recall)
print(f"precision={precision:.3f}")
print(f"recall={recall:.3f}")
print(f"F1={f1:.3f}")Practical options
The bert_score package obtains embeddings from a model selected for the language. It returns precision, recall, and F1 for each sentence pair.
IDF weighting gives rarer tokens more influence than common tokens. This can reduce the effect of frequent words, but it requires a chosen corpus and makes comparisons less direct. Baseline rescaling maps raw scores using expected scores from ordinary comparison pairs. It improves readability, but it does not change the underlying token matches.
Exercise
Why can several candidate tokens match the same reference token in BERTScore?
Show answer
Matching is greedy and independent for each token. Each candidate token selects its highest-similarity reference token without reserving that token for other matches.