Sub topic 13
BERTScore Options: IDF Weighting and Baseline Rescaling
This chapter assumes you know how BERTScore matches tokens greedily. Each candidate token takes its best cosine similarity against the reference tokens, and the reverse for recall. It also assumes you can read basic Python. Two options change what the resulting numbers mean: IDF weighting and baseline rescaling. They solve different problems. IDF weighting changes which tokens count most. Baseline rescaling changes the scale on which scores are reported.
What each option changes
| Option | Where it acts | Changes ranking of systems? | Main purpose |
|---|---|---|---|
| IDF weighting (`idf=True`) | Inside the average over tokens | Often, slightly | Make rare, informative tokens count more than common ones |
| Baseline rescaling (`rescale_with_baseline=True`) | After P, R, and F1 are computed | No, it is a linear map | Spread scores over a readable range |
The diagram shows where each option sits in the pipeline.
Why plain averaging overweights common words
Plain BERTScore gives every token the same weight. In a reference such as "the cat sat on the mat", the two "the" tokens count as much as "mat". Function words are easy to match, because almost any candidate contains one that is close in embedding space. They therefore push scores up without telling you whether the content is right.
IDF stands for inverse document frequency. A token that appears in many references carries little information, and a token that appears in few carries more. BERTScore computes IDF from the reference sentences you supply. With M references and a token w that appears in df(w) of them, the bert_score package uses:
idf(w) = log((M + 1) / (df(w) + 1))The paper writes the same idea without the +1 terms. The package adds them so that a token seen in every reference gets weight 0 and an unseen token does not cause a division by zero. The weighted recall for a reference x and candidate x̂ is then:
R = sum over reference tokens x_i of idf(x_i) * max_j cos(x_i, x̂_j)
---------------------------------------------------------------
sum over reference tokens of idf(x_i)Precision uses the same formula with the roles swapped. It weights each candidate token by its IDF and takes the best match in the reference. F1 is still the harmonic mean of P and R.
Worked example: IDF from scratch
The following code runs without a model. It computes IDF weights from four reference sentences, then compares a plain recall with an IDF-weighted recall. The per-token best-match similarities are made up. In real use they come from the embedding model.
import math
from collections import Counter
references = [
"the cat sat on the mat".split(),
"the dog sat on the rug".split(),
"a bird flew over the house".split(),
"the cat chased the bird".split(),
]
def idf_weights(refs):
m = len(refs)
doc_freq = Counter()
for ref in refs:
doc_freq.update(set(ref)) # count each token once per reference
return {w: math.log((m + 1) / (c + 1)) for w, c in doc_freq.items()}
idf = idf_weights(references)
print({w: round(v, 3) for w, v in sorted(idf.items())})
# Pretend best-match cosine similarities for reference 1, one per token.
ref = references[0]
best_sim = [0.90, 0.95, 0.60, 0.90, 0.90, 0.50]
plain = sum(best_sim) / len(best_sim)
weights = [idf[w] for w in ref]
weighted = sum(w * s for w, s in zip(weights, best_sim)) / sum(weights)
print(round(plain, 3), round(weighted, 3)) # 0.792 0.698"the" occurs in all four references, so its weight is log(5/5) = 0. "mat" occurs in one reference, so its weight is log(5/2) ≈ 0.916. The poorly matched "mat" (similarity 0.50) now pulls the recall down from 0.792 to 0.698, and the well-matched "the" tokens no longer help.
Edge cases for IDF
- IDF is computed from the reference set you pass in, not from a general corpus. With a handful of references the weights are noisy. With a single reference every token has the same document frequency, so the weights are all equal and IDF has no effect.
- The weights depend on the test set. The same system scored on two different test sets uses different weights, so IDF scores are not strictly comparable across sets.
- Tokens are subword pieces from the model's tokenizer, so IDF is computed over pieces, not whole words.
- The
bert_scorepackage gives special tokens such as the start and end markers zero weight when IDF is on. Do not assume that is true of other implementations. - The BERTScore authors found IDF helps in some settings and hurts in others. Treat it as an option to test on your task rather than a default.
Why raw BERTScore sits in a narrow band
Contextual embeddings from models such as RoBERTa are not spread evenly over the space. Any two tokens tend to have a high cosine similarity, so even two unrelated sentences get a high raw BERTScore. In practice raw F1 for English with the default model often falls in a narrow high band, for example 0.8 to 1.0. A raw difference of 0.02 between two systems is then hard to interpret.
Baseline rescaling fixes the readability problem. The BERTScore authors computed the average score on a large number of random candidate and reference pairs. They did this once for each language and model layer. This average is the baseline b. The reported score is:
rescaled = (raw - b) / (1 - b)This is applied separately to P, R, and F1, each with its own baseline. A score equal to the baseline maps to 0, and a perfect score of 1 stays at 1. The map is linear and increasing, so it never changes which system ranks higher. It only stretches the differences. The table uses an illustrative baseline of b = 0.83. Real baselines depend on the model, the layer, and the language.
| Raw score | Rescaled score (b = 0.83) |
|---|---|
| 0.83 | 0.000 |
| 0.90 | 0.412 |
| 0.95 | 0.706 |
| 1.00 | 1.000 |
| 0.80 | -0.176 |
The last row shows that rescaled scores can be negative. A pair that is less similar than a random pair gets a negative value. Rescaled scores are also not guaranteed to stay below 1 on every pair, though they usually do.
def rescale(raw, baseline):
return (raw - baseline) / (1 - baseline)
for raw in [0.83, 0.90, 0.95, 1.00, 0.80]:
print(raw, round(rescale(raw, 0.83), 3))Using both options in the library
The score function takes both options as flags. The language selects the default model and the matching baseline file.
from bert_score import score
candidates = [
"The cat sat on the mat.",
"A dog is in the garden.",
"It rains today.",
]
references = [
"A cat was sitting on the rug.",
"There is a dog in the garden.",
"Today there is rain.",
]
(P, R, F1), hash_code = score(
candidates,
references,
lang="en",
idf=True,
rescale_with_baseline=True,
return_hash=True,
)
print(F1.mean().item())
print(hash_code) # model, layer, idf and rescaling settings, and library versionReport the hash string together with the score. It records the model, the layer, whether IDF and rescaling were on, and the library version. Without it, a BERTScore number cannot be reproduced or compared with someone else's.
Practical guidance
- Decide on the settings before you look at results, and keep them fixed across all systems you compare.
- Turn on rescaling when humans will read the numbers. It does not change rankings, so it is safe for system comparison and only improves readability.
- Try IDF on a validation set with human judgments if you have one. Keep it only if its correlation with those judgments is better.
- Compute IDF over a test set large enough to give stable weights, and report that the test set was used for the weights.
- Always log the hash string with the numbers.
Exercise
A system gets a raw BERTScore F1 of 0.88. The baseline for the model, layer, and language you use is b = 0.82. What is the rescaled F1? Another system has raw F1 0.85. Does rescaling change which system ranks higher?
Show answer
Rescaled F1 = (0.88 - 0.82) / (1 - 0.82) = 0.06 / 0.18 ≈ 0.333. The second system gets (0.85 - 0.82) / 0.18 ≈ 0.167. The first system still ranks higher, because rescaling is a linear increasing map and cannot change the order.
Exercise
You compute IDF weights from a test set that has only one reference sentence. What do the weights look like, and what does that mean for idf=True?
Show answer
With M = 1, every token in that sentence has document frequency 1, so every weight is log(2/2) = 0. All weights are equal, and the weighted average is undefined if the code divides by the sum of weights. In practice IDF needs several references to give a useful weighting, so it should not be used on a single sentence.