How BERTScore works: matching candidate and reference tokens
Learn how contextual token embeddings produce BERTScore precision, recall, and F1, with a worked similarity-matrix example and reproducibility settings.
How BERTScore works
BERTScore is an automatic metric that compares generated text with a human reference using contextual embeddings from a pretrained language model. It scores how similar each token is to its best match in the other sentence, then reports precision, recall, and F1. It can credit different wording, but scores depend on the model, layer, tokenizer, and options.
From token similarities to a sentence score
The model represents each token as a vector influenced by its context. BERTScore calculates cosine similarity between reference and candidate tokens. For precision, each candidate token takes its highest similarity with any reference token; for recall, each reference token takes its highest similarity with any candidate token. F1=2PR/(P+R). These matches are independent, not a one-to-one assignment: several tokens may select the same counterpart. This can capture paraphrase or word-order variation, but does not guarantee that negation, roles, or factual meaning are correct.
Illustrative calculation
The matrix below is hypothetical, not model output. Rows are candidate tokens; columns are reference tokens.
| Candidate / reference | A | B |
|---|---|---|
| 1 | 0.90 | 0.20 |
| 2 | 0.40 | 0.80 |
| 3 | 0.70 | 0.30 |
Candidate-side maxima are 0.90, 0.80, 0.70, so P=(0.90+0.80+0.70)/3=0.80. Reference-side maxima are 0.90 and 0.80, so R=0.85. F1=2×0.80×0.85/(0.80+0.85)≈0.82. Different candidate and reference lengths can yield different P and R.
Fix settings before comparing
The metric is not tied to one BERT checkpoint. Model, layer, tokenizer, and software versions can change similarities. Optional inverse document frequency (IDF) weights rare reference tokens more heavily; on a small or narrow reference set, those weights may be unstable. Optional baseline rescaling makes scores easier to read but changes their numeric scale; do not mix rescaled and raw values.
The official package can score line-aligned files. It may download model weights on first use; the example uses English and a specified RoBERTa checkpoint/layer, so do not transfer it unchanged to another language:
python -m pip install bert-score
bert-score -r refs.txt -c hyps.txt --lang en -m roberta-large -l 17 --idf --rescale_with_baselineThe output includes P, R, F1, and a settings hash. Save the package and Transformers versions, model/layer, language, IDF and rescaling choices, and reference files. The original paper found stronger human-judgment correlations in its evaluated translation and image-captioning settings; that is not a guarantee for every task. For the broader sentence-similarity task, see STS score interpretation.
References
[Zhang et al. (2020), BERTScore](https://openreview.net/forum?id=SkeHuCVFDr)
[Official BERTScore implementation](https://github.com/Tiiiger/bert_score)
[Hugging Face Evaluate: BERTScore limitations](https://github.com/huggingface/evaluate/tree/main/metrics/bertscore)