Your F1 score is 0.75 in torchmetrics, 0.70 in torcheval, a vector in Keras and an error in sklearn. Which one is right, and why?
metric-diff computes the same metric with scikit-learn, torchmetrics, torcheval, Keras 3, Hugging Face evaluate and MONAI (segmentation), for text generation and retrieval with sacrebleu, nltk, rouge-score and pytrec_eval (trec_eval), and for calibration error with uncertainty-calibration. When they disagree, it finds the setting that causes it, re-runs every library with that setting aligned to confirm, and tells you how to fix your code.
It also answers the other classic question: is the number I logged per batch the number for my dataset?
New here? The user guide is organised by what you're seeing: "my training-loop F1 doesn't match sklearn", "all my metrics are the same number", "my Dice differs from MONAI", "will upgrading torchmetrics change my numbers?" and eight more scenarios, each with a snippet to run.
$ metric-diff auroc --true labels.csv --pred logits.csv
metric-diff: auroc (binary, 15 samples, predictions look like logits (values outside [0, 1]))
sklearn 0.9286 (1.9.1)
torchmetrics 0.5000 (1.9.0) <-
torcheval 0.9286 (0.0.7)
keras 0.5000 (3.15.1) <-
MISMATCH: explained by 1 cause (confirmed)
1. Large logits saturate when squashed to probabilities [saturation]
torchmetrics applies sigmoid to logits in float32: the 15 distinct scores collapse to 1
distinct value (large logits all become 1.0), so their ranking is lost and auroc drifts
toward 0.5. sklearn and torcheval rank the raw scores. Keras AUC also assumes
probabilities and clips values outside [0, 1] (use from_logits=True).
fix: For ranking metrics pass the raw scores to sklearn, or rescale monotonically
before torchmetrics, e.g. torch.sigmoid((logits - logits.mean()).double()).
see: Lightning-AI/torchmetrics#2819
confirmed: after aligning these settings, every library reports 0.9286
Metric libraries look interchangeable but make different silent choices. Developers keep filing "wrong result" bugs that turn out to be defaults (torchmetrics#746, torcheval#191, keras#5400), and some are real bugs (torchmetrics#2819, torchmetrics#3518, evaluate#869). In text generation, the same BLEU score is famously not comparable between papers because of tokenisation and smoothing choices (Post 2018). metric-diff tells defaults and bugs apart in seconds.
pip install metric-diff # sklearn only (reference)
pip install "metric-diff[torch]" # + torchmetrics and torcheval
pip install "metric-diff[keras]" # + Keras 3
pip install "metric-diff[evaluate]" # + Hugging Face evaluate
pip install "metric-diff[monai]" # + MONAI (segmentation)
pip install "metric-diff[text]" # + sacrebleu, nltk, rouge-score (BLEU, chrF, ROUGE)
pip install "metric-diff[ranking]" # + pytrec_eval (trec_eval: NDCG, MAP, MRR, @k)
pip install "metric-diff[calibration]" # + uncertainty-calibration (ECE)
pip install "metric-diff[all]" # everythingHugging Face evaluate downloads each metric script from the Hugging Face Hub the first time it is used (cached afterwards), so that backend needs internet access once. torchmetrics' ROUGE-Lsum downloads nltk's small punkt_tab sentence splitter the first time, the same way.
Keras runs on whichever backend you have. If TensorFlow isn't installed and you haven't set KERAS_BACKEND, metric-diff uses the torch backend.
from metric_diff import compare
report = compare("f1", y_true, y_pred) # lists, numpy arrays or torch tensors
print(report) # human-readable explanation
report.agree # True / False
report.causes # list of Cause(key, title, detail, fix, reference)
report.to_dict() # JSON-serialisableBy default each library is called with its own defaults. Pass the settings your code uses:
compare("f1", y, probs, average="weighted") # every library, same setting
compare("precision", y, scores, threshold=0.3)
compare("f1", y, p, pos_label=2) # binary labels {1, 2}
compare("f1", y, p, num_classes=10, average="macro") # some classes never occur
compare("precision", y, p, average="macro", zero_division=1) # value used for 0/0
compare("accuracy", y, probs, top_k=5) # top-k accuracy
# Reproduce code that calls each library differently:
compare("f1", y, probs, settings={
"sklearn": {"average": "weighted"},
"torchmetrics": {"average": "macro"},
})If a library can't do what you asked (torcheval has no weighted AUROC, for example), it is shown as skipped with the reason, not counted as a mismatch.
metric_diff.testing turns any comparison into an assertion, for pytest, unittest or plain scripts. A failing test prints the full explanation:
from metric_diff.testing import assert_metrics_agree, assert_batches_consistent
def test_val_f1_is_library_independent():
assert_metrics_agree("f1", y_val, scores, average="macro")
def test_known_difference_only():
# passes only if the libraries agree, or differ *only* for a reason you accept
assert_metrics_agree("precision", y, scores, threshold=0.3, allow=["threshold_tie"])
def test_my_own_metric():
assert_metrics_agree("f1", y, p, expected=my_f1(y, p)) # your code vs every library
def test_epoch_metric():
assert_batches_consistent("f1", val_batches, expected_n=len(val_dataset))allow takes the keys shown in brackets in a report ([threshold_tie]); a typo raises an error instead of silently allowing nothing. By default at least two libraries must compute the metric (min_libraries=2), so a test can't pass just because only one library is installed.
from metric_diff import compare_batched
report = compare_batched("f1", [(y_batch, p_batch) for y_batch, p_batch in val_batches],
expected_n=len(val_dataset))
print(report)metric-diff batched: f1 (binary, 1000 samples in 16 batches, batch sizes 40-64)
whole dataset (sklearn) 0.6558 reference
mean of per-batch values 0.6537 naive average of batch scores
batch-size-weighted mean 0.6530 Lightning self.log(value, on_epoch=True)
torchmetrics update()/compute() 0.6558 accumulated
torcheval update()/compute() 0.6558 accumulated
PROBLEM: 1 issue with how the metric is aggregated
1. Averaging per-batch values is not the dataset metric [batch_average]
...
It reports:
- averaging per-batch values (plain or batch-size-weighted) vs. computing on the whole dataset
- batches left out because the metric is undefined for them (e.g. one class only)
- a library whose
update()/compute()doesn't match its own one-shot result - with
expected_n: samples counted twice (DistributedSampler padding) or dropped (drop_last=True)
Pass y_true as one 0/1 column per label, and y_pred in the same shape (labels, probabilities or logits):
compare("accuracy", y_true, y_pred) # y_true.shape == y_pred.shape == (n_samples, n_labels)
compare("f1", y_true, y_pred, average="macro") # also "micro", "weighted", "samples"
compare("accuracy", y_true, y_pred, exact_match=False)metric-diff: accuracy (multilabel, 3 labels, 8 samples, predictions look like probabilities)
sklearn 0.6250 (1.9.1)
torchmetrics 0.8750 (1.9.0) <-
torcheval 0.6250 (0.0.7)
keras 0.8750 (3.15.1) <-
evaluate 0.6250 (0.4.6)
MISMATCH: explained by 1 cause (confirmed)
1. Subset accuracy vs per-label accuracy [multilabel_accuracy]
On multilabel data 'accuracy' means two different things. Subset (exact-match) accuracy
counts a sample as correct only if every label is right (sklearn, torcheval, evaluate);
per-label accuracy scores every (sample, label) cell separately (torchmetrics, keras). ...
Multilabel covers accuracy, precision, recall, f1, auroc and average_precision. Other causes it confirms: sklearn and evaluate raise without average, while torchmetrics uses macro and Keras pools every cell (micro); Keras F1Score() without a threshold keeps only the top label of each sample; and a label with no positives makes sklearn's AUROC NaN, while torchmetrics and Keras count it as 0.
mcc, cohen_kappa (with weights="linear" or "quadratic"), balanced_accuracy and specificity take labels or scores like F1 does. brier, log_loss and ece (expected calibration error) take probabilities:
compare("mcc", y_true, y_pred)
compare("cohen_kappa", y_true, y_pred, weights="quadratic")
compare("log_loss", y_true, probabilities)
compare("ece", y_true, probabilities, n_bins=15)They inherit the existing checks (thresholds, ties on the threshold, labels that aren't {0, 1}, logits) and add their own: torchmetrics returns MCC = 1.0 where sklearn returns 0.0 when everything is one class; torchmetrics' macro accuracy counts classes that are predicted but absent from y_true (0.50 vs 0.75 for sklearn's balanced accuracy); sklearn and Keras clip probabilities of exactly 0 or 1 at different points (one confident mistake in ten samples: log loss 4.03 vs 2.02); evaluate's Brier score on labels {1, 2} is 0.48 instead of 0.14 (evaluate#869); and for ECE, torchmetrics and uncertainty-calibration put a confidence that falls exactly on a bin edge into different bins (0.275 vs 0.195 when one score is 0.6 = 9/15).
Pass integer label maps of shape (n_images, *spatial), e.g. (N, H, W) or (N, D, H, W):
compare("dice", true_masks, pred_masks) # each library's defaults
compare("iou", true_masks, pred_masks, aggregation="per_image", include_background=False)
compare("iou", true_masks, pred_masks, ignore_index=255) # leave out 'void' pixelsignore_index leaves out the pixels whose target is that label, the way each library's users do it: Keras MeanIoU(ignore_class=), evaluate mean_iou(ignore_index=), and masking for sklearn, torchmetrics and MONAI. With it, Keras and evaluate can join comparisons on datasets such as ADE20K or Cityscapes.
On the same 3 images, "mean IoU" is 0.653 in sklearn, Keras and evaluate (all pixels pooled), 0.631 in torchmetrics (per image) and 0.735 in MONAI (per image, and false positives on images without the class are ignored). metric-diff names and confirms both causes.
y_true = references (a string per sample, or a list of strings for several references), y_pred = generated texts:
compare("bleu", references, generated)
compare("bleu", references, generated, tokenize="13a", smooth=False) # settings for every library
compare("chrf", references, generated, word_order=2) # chrF++
compare("rougeLsum", references, generated, use_stemmer=True)metric-diff: bleu (text, 3 generated texts, 1 reference each)
torchmetrics 0.3289 (1.9.0)
evaluate 0.3916 (0.4.6) <-
sacrebleu 39.1597 (2.6.0) <-
nltk 0.3206 (3.10.4) <-
MISMATCH: explained by 3 causes (confirmed)
1. Different scales: 0-100 vs 0-1 [text_scale]
2. Different tokenisation [bleu_tokenize]
... The 13a tokenizer (sacrebleu, evaluate's bleu) splits off punctuation: 'mat.' becomes
'mat .'; torchmetrics BLEUScore and nltk (called on text.split()) split on spaces only ...
3. nltk counts texts shorter than 4 words differently [nltk_short_hypotheses]
Settings: tokenize (13a, whitespace, intl, char, zh), smooth, lowercase, aggregation (corpus or sentence mean), word_order (chrF), use_stemmer (ROUGE).
One row per query: y_true = relevance of each candidate (0 = not relevant, graded values for NDCG), y_pred = the model's scores. A 2-D array, or a list of rows of different lengths:
compare("ndcg", relevance, scores, top_k=10)
compare("map", relevance, scores)
compare("precision_at_k", relevance, scores, top_k=5)
compare("mrr", relevance, scores, empty_queries="skip") # queries without a relevant documentOn three queries with tied scores, MAP is 0.506 in sklearn, 0.585 in trec_eval and 0.678 in torchmetrics on Windows: each orders ties differently, and torchmetrics' order even changes by platform (0.585 on Linux, 0.706 on macOS). With raw logits as scores, torchmetrics' MAP, MRR, precision and recall drop every candidate scored 0 or below (torchmetrics#3518). metric-diff confirms both.
.csv, .txt or .npy, with one column per class for multiclass scores. Text metrics read one text per line (repeat --true for several references, as sacrebleu does); ranking metrics read one query per line:
metric-diff f1 --true y_true.csv --pred y_pred.csv
metric-diff auroc --true y.npy --pred scores.npy --json
metric-diff f1 --true y.csv --pred p.csv --batch-size 64 --expected-n 1000
metric-diff f1 --true y.csv --pred p.csv --backends sklearn,keras
metric-diff f1 --true y.csv --pred p.csv --average weighted
metric-diff precision --true y.csv --pred p.csv --set sklearn.threshold=0.3 --set torchmetrics.threshold=0.5
metric-diff bleu --true refs.txt --true refs2.txt --pred generated.txt --tokenize 13a
metric-diff ndcg --true relevance.csv --pred scores.csv --top-k 10Settings flags: --average, --threshold, --pos-label, --num-classes, --zero-division, --top-k, --exact-match, --weights, --n-bins, --include-background, --aggregation, --ignore-index, --tokenize, --smooth, --lowercase, --word-order, --use-stemmer, --empty-queries, --adaptive-k, and --set LIB.KEY=VALUE (repeatable) for per-library overrides. For multilabel data, --true and --pred hold one column per label.
Exit code: 0 = OK, 1 = mismatch or aggregation problem, 2 = bad input, so it can fail a CI job.
compare() answers "why do my numbers disagree?" for your data. The conformance matrix asks the question in advance: it runs a library of known edge cases (69 today, covering binary, multiclass and multilabel classification, probabilities, regression, segmentation, text generation and ranking, each linked to the upstream issue where there is one) on every installed library, and shows who agrees and who doesn't.
metric-diff matrix # table in the terminal
metric-diff matrix --format markdown --output metrics-matrix.md
metric-diff matrix --save baseline.json # remember today's results
metric-diff matrix --baseline baseline.json # what changed since? exit 1 on drift
metric-diff matrix --case seg- --case auroc # only some cases
metric-diff matrix --list # the edge casesAn excerpt (sklearn 1.9.1, torchmetrics 1.9.0, torcheval 0.0.7, Keras 3.15.1, evaluate 0.4.6, MONAI 1.6.2):
case sklearn torchmetrics torcheval keras evaluate monai confirmed cause
bin-auroc-large-logits 0.9286 0.5000 x 0.9286 0.5000 x 0.9286 saturation
bin-f1-labels-1-2 0.8000 error 1.1111 x 1.1111 x 0.8000 pos_label
mc-accuracy-default 0.7000 0.6667 x 0.7000 0.7000 0.7000 average
mc-top2-ties 1.0000 0.3333 x 1.0000 1.0000 - top_k_ties
reg-mape 0.2805 0.2805 - 28.0476 x 0.2805 mape_percent
reg-r2-large-values 0.9890 1.0000 x 1.0000 x 1.0000 x 0.9890 float32_precision
seg-iou-default 0.6530 0.6308 x 0.6530 0.6530 0.7350 x monai_ignore_empty, seg_aggregation
x = differs from sklearn (or from the majority when sklearn can't compute it), - = the library can't do this.
- Upgrade safety. Save a baseline, then re-run after upgrading a library. If torchmetrics 1.9 -> 2.0 changes AUROC on saturated logits, or macro F1 with an unseen class, you see that exact cell change, the day you upgrade instead of months later when results quietly shift between experiments.
- Choosing or switching libraries. Moving a pipeline from Keras to PyTorch, or from sklearn to torchmetrics? See every known difference before you migrate, not one bug report at a time.
- Reproducible reporting. Attach the Markdown matrix to a paper, model card or internal report: "computed with torchmetrics 1.9, which differs from sklearn in these documented ways". Reviewers and other teams can compare numbers fairly.
- Spotting likely bugs. Red cells on control cases point at library behaviour worth a closer look: the open torchmetrics#2819 and #3518, and F1 values above 1 from torcheval and Keras on labels {1, 2} (reported as torcheval#241 and keras#24360), show up exactly like that.
- Shared knowledge. Every reported mismatch becomes a new edge case, so the matrix keeps growing and everyone's CI checks it.
| You are | Use | How |
|---|---|---|
| A developer debugging a number today | compare() |
on your own data |
| An ML platform / MLOps team | matrix --baseline |
in CI, on every dependency upgrade |
| A researcher reporting results | both | attach matrix --format markdown to the paper |
| A metric library maintainer | matrix |
a cross-library test suite that points at regressions |
The GitHub Action re-runs the matrix on the library versions your project installs and fails the job if any result changed:
# .github/workflows/metric-drift.yml
name: metric drift
on: [pull_request] # e.g. a Dependabot or Renovate PR that bumps torchmetrics
jobs:
metrics:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt # your pinned library versions
- uses: DadCoder001/metric-diff@v0.2.0a1
with:
baseline: metric-baseline.json- First run: there's no baseline yet, so the action saves one and uploads it as the
metric-baselineartifact. Download it and commit it. - Every run after that: the job page gets a summary of what changed (library versions, and each result that changed value, started failing or started working) with the full matrix underneath.
- After an intended upgrade: run with
update-baseline: trueand commit the new baseline. - Options:
extras(defaultall; libraries you installed first keep their versions),cases,backends,fail-on-drift: falseto only warn. The outputsdriftandbaseline-createdcan drive later steps. - One baseline per OS: some libraries order tied scores differently on Linux, macOS and Windows, so use a separate baseline per runner OS (
baseline: metric-baseline-${{ runner.os }}.json). The report warns if the baseline came from another OS.
Without GitHub Actions: metric-diff matrix --save metric-baseline.json once, then metric-diff matrix --baseline metric-baseline.json (exit code 1 on drift; --summary FILE appends a Markdown report).
| Cause | Example |
|---|---|
| Different default averaging | torchmetrics macro, torcheval micro, sklearn binary (error on multiclass); torchmetrics macro accuracy = sklearn balanced accuracy |
| Binary logits vs probabilities | torchmetrics applies sigmoid automatically; torcheval and Keras threshold raw logits at 0.5 |
| Sigmoid saturation of large logits | torchmetrics AUROC = 0.5 instead of 0.93 (torchmetrics#2819) |
| Multiclass logits vs probabilities | torchmetrics softmaxes, torcheval ranks raw logits, sklearn raises |
| Scores exactly on the threshold | torchmetrics and Keras use > 0.5, torcheval uses >= 0.5 |
Binary labels not {0, 1} |
sklearn pos_label=1, torchmetrics raises, torcheval and Keras return F1 > 1 |
| Probabilities that don't sum to 1 | sklearn multiclass AUROC raises |
Keras F1Score() without threshold |
binary F1 scored as if every sample were predicted positive |
Keras multiclass Precision() / Recall() |
each class column thresholded at 0.5 like multi-label data, not argmax (use top_k=1) |
Keras AUC() with logits |
values clipped to [0, 1] unless from_logits=True |
Keras AUC() 200 thresholds |
AUROC is approximated; small but real differences |
Keras AUC(curve="PR") |
interpolated PR-AUC, not average precision |
Different average / threshold per library (your settings) |
e.g. sklearn weighted vs torchmetrics macro |
sklearn average= on binary data |
averages both classes; torch/Keras binary metrics score the positive class only |
num_classes with classes that never occur |
sklearn (labels=) and Keras count them as 0; torchmetrics and torcheval leave them out |
sklearn multiclass AUROC without multi_class |
raises unless multi_class="ovr" |
Different zero_division |
what 0/0 becomes; torcheval and Keras always use 0 |
| Tied scores at the top-k cut-off | torchmetrics keeps exactly k classes, and which tied one depends on the platform (0.33 on Windows/macOS, 0.67 on Linux for the same data); sklearn, torcheval and Keras count any tied class as a hit |
| Multilabel: subset vs per-label accuracy | sklearn, torcheval and evaluate: every label right (0.625); torchmetrics and Keras: each label separately (0.875) |
| Multilabel: default averaging | sklearn and evaluate raise, torchmetrics uses macro, Keras pools every cell (micro) |
Multilabel: Keras F1Score() without threshold |
keeps only the top label of each sample: macro F1 0.694 instead of 0.869 |
| Multilabel: a label with no positives | AUROC is NaN in sklearn (so the average is NaN); torchmetrics and Keras count it as 0 |
| MCC when everything is one class | sklearn and evaluate 0.0, torchmetrics 1.0 |
| Balanced accuracy with classes only in the predictions | sklearn and torcheval average the true classes (0.75); torchmetrics' macro accuracy also counts the extra class (0.50) |
| Log loss: probabilities of exactly 0 or 1 | sklearn clips at 2.2e-16, Keras at 1e-7: 4.03 vs 2.02 with one confident mistake in ten |
| Log loss: logits | sklearn raises; Keras treats them as probabilities unless from_logits=True |
| Brier score: labels {1, 2} | evaluate 0.48 vs sklearn 0.14 (evaluate#869) |
| ECE: confidences exactly on a bin edge | torchmetrics bins are [a, b), uncertainty-calibration's (a, b]: 0.275 vs 0.195 |
ECE: logits, or a different n_bins |
torchmetrics applies sigmoid / softmax, uncertainty-calibration uses the raw values (ECE above 1) |
| Per-batch averaging, padding, dropped samples | see compare_batched above |
| Regression: Keras MAPE is a percentage | 28.0 in Keras vs 0.28 in sklearn and torchmetrics |
| Regression: MAPE with zero targets | each library divides by a different tiny epsilon: 7.5e14 vs 142,450 vs 1.7e8 |
| Regression: R2 with a constant target | torcheval and evaluate return -inf (torcheval NaN if perfect); others return 0.0 (1.0) |
| Segmentation: per-image vs whole-dataset averaging | torchmetrics and MONAI average per image; sklearn, Keras and evaluate pool all pixels |
Segmentation: MONAI ignore_empty=True |
false positives on images where the class is absent are ignored, so the score looks better |
| Segmentation: background counted or not | include_background differs between your calls |
| Segmentation: ignored pixels | ignore_index differs between your calls; label 255 without ignore_index gets a note |
Segmentation: torchmetrics input_format |
label maps without input_format="index" silently give a wrong number (shown as a note) |
evaluate r_squared rounds to 3 decimals |
0.959 vs 0.9595 |
| Regression: float32 rounding on large values | R2 of 1.0000001 instead of 0.989 when values are about 1e6 (torchmetrics#3077) |
| Text: 0-100 vs 0-1 | sacrebleu (BLEU, chrF) and evaluate (chrF) report 0-100 |
| Text: BLEU tokenisation | 13a (sacrebleu, evaluate) vs whitespace (torchmetrics BLEUScore, nltk): 39.2 vs 32.9 on the same text |
| Text: BLEU smoothing | sacrebleu smooths by default; the others don't, and each smooths differently when asked |
| Text: corpus vs mean of sentence BLEU/chrF | 0.11 vs 0.32 (nltk) on the same three sentences |
| Text: nltk and short texts | nltk counts a text shorter than 4 words as having unmatched n-grams, lowering corpus BLEU |
| Text: chrF vs chrF++ | torchmetrics CHRFScore defaults to chrF++ (n_word_order=2) |
| Text: chrF averaging | sacrebleu averages precision/recall over n-gram orders, torchmetrics averages F-scores (eps_smoothing) |
| Text: ROUGE-Lsum sentence splitting | torchmetrics splits sentences with nltk (0.87); rouge-score and evaluate only on newlines (0.43) |
| Text: evaluate ROUGE bootstrap | use_aggregator=True reports a resampled estimate, not the mean, and can change between runs |
| Text: stemming, lowercasing per library | your settings differ between calls |
| Text: non-Latin text in ROUGE | every library drops the characters by default and scores 0 (shown as a note) |
| Ranking: tied scores | sklearn averages over tie orders, torchmetrics keeps its sort order, trec_eval sorts ties by document id |
| Ranking: scores of 0 or below | torchmetrics MAP/MRR/precision/recall treat them as not retrieved (torchmetrics#3518) |
| Ranking: MAP@k denominator | trec_eval divides by all relevant documents, torchmetrics by those found in the top k |
| Ranking: queries without relevant documents | counted as 0, skipped or counted as 1 |
| Ranking: precision@k on short lists | divide by k, or by the list length (adaptive_k / limit_k_to_size) |
Metrics:
- Classification (binary and multiclass):
accuracy,precision,recall,f1,auroc,average_precision,mcc,cohen_kappa,balanced_accuracy,specificity(torcheval and Keras have none of the last four, except torcheval's macro accuracy for balanced accuracy; multiclass specificity is torchmetrics only) - Multilabel:
accuracy,precision,recall,f1,auroc,average_precision(torcheval has accuracy and AP only) - Probabilities:
brier(sklearn, evaluate; multiclass sklearn only),log_loss(sklearn, Keras),ece(torchmetrics, uncertainty-calibration; top-label for multiclass) - Segmentation (label maps, 2-D or 3-D):
dice,iou(Keras and evaluate have IoU only; torcheval has none) - Regression (single or multi-output):
r2,mae,mse,rmse,mape(torcheval only hasr2andmse; evaluate is single-output only) - Text generation:
bleu,chrf(sacrebleu, nltk for BLEU, torchmetrics, evaluate),rouge1,rouge2,rougeL,rougeLsum(rouge-score, torchmetrics, evaluate) - Ranking / retrieval:
ndcg,map(sklearn, torchmetrics, pytrec_eval),mrr,precision_at_k,recall_at_k,hit_rate(torchmetrics, pytrec_eval; torcheval for precision@k)
evaluate has no average precision, zero_division or top-k; those cases show it as skipped.
How a cause is confirmed: each detector knows how to align one setting (for example pass average="macro" everywhere, or apply softmax first). metric-diff re-runs all libraries with the alignment and only says confirmed if they then agree. If nothing reproduces agreement, it says so: that is often a new bug worth reporting.
Beyond the hand-made edge cases, metric-diff runs on real model predictions: logistic regression and ridge models trained on public datasets bundled with scikit-learn (breast_cancer, digits, wine, diabetes), evaluated on a 30% test split. Full tables: benchmarks/RESULTS.md; reproduce with python benchmarks/real_data.py (no downloads, about a minute).
What showed up on real data:
- Keras F1 on real probabilities: 0.787 instead of 0.967 (breast_cancer), because
F1Score()has no threshold by default. - Keras AUROC on real logits: 0.965 instead of 0.996 (breast_cancer) and 0.848 instead of 0.999 (digits, 10 classes), because Keras clips scores to [0, 1].
- Averaging per-batch R2 (batch size 32): 0.223 instead of 0.365 (diabetes), and per-batch F1 slightly off on every dataset.
- Keras MAE / MSE / MAPE accumulated over batches with 1-D targets average the batch means, not the samples, so the result is wrong whenever the last batch is smaller; targets shaped
(batch, 1)are fine. Found on the diabetes predictions. - torchmetrics' macro default for multiclass accuracy: 0.9721 vs 0.9722 on digits. Small, but a different number in a results table.
- When the predictions are clean probabilities and settings are explicit, all five libraries agree to 4 decimals.
And on pretrained Hugging Face models (benchmarks/RESULTS_HF.md; python benchmarks/download_hf.py fetches about 720 MB once, then python benchmarks/hf_data.py runs offline):
- DistilBERT on SST-2 (872 sentences): Keras F1 0.675 instead of 0.914; Keras AUROC 0.959 on probabilities (its 200 fixed thresholds can't resolve confident predictions bunched near 0 and 1) and 0.911 on logits, vs 0.972 everywhere else; Keras PR-AUC 0.943 vs average precision 0.973. On the model's 2-column output, sklearn raises and wants
scores[:, 1]. - SegFormer on ADE20K (200 validation images, 150 classes): the same predictions give Dice 0.284 (all pixels pooled: sklearn), 0.388 (per image: torchmetrics) and 0.538 (MONAI's
ignore_empty=Truedefault); IoU 0.209 / 0.335 / 0.464. MONAI's default alone adds about 0.15 by ignoring false positives on images without the class. - torchmetrics' own two IoU APIs disagree: the
MeanIoUclass gives 0.304 and themean_ioufunction 0.335 on the same masks, because the class pools all (image, class) pairs while the function averages per image. Found by the batch check; not caused by batching. Reported as torchmetrics#3660. - Running on real data also made metric-diff faster: segmentation now processes images in chunks (one-hot masks over 150 classes would need gigabytes) and uses MONAI's label-map Dice path (3x faster, identical values).
The newer metrics run on real outputs too (benchmarks/RESULTS_NEW.md, no new downloads; benchmarks/RESULTS_TEXT_RANKING.md, about 395 MB of public models and data). Of the 104 comparisons, 100 involve at least two libraries, and every one of those either agrees or is explained and confirmed (the other 4, multiclass specificity and Brier score, have a single library):
- Multilabel (digit images with three derived labels, real classifier): accuracy 0.785 (subset: sklearn, torcheval, evaluate) vs 0.919 (per label: torchmetrics, Keras); Keras
F1Score()without a threshold 0.722 instead of 0.915. - Calibration (DistilBERT on SST-2, breast_cancer, digits, wine): torchmetrics and uncertainty-calibration agree on probabilities; on logits uncertainty-calibration reports an ECE of 7.08; averaging ECE over batches of 32 gives 0.095 instead of 0.080.
- Log loss on hard labels: 1.48 in sklearn vs 0.66 in Keras (different clipping). MCC, kappa, balanced accuracy, specificity and the Brier score agree on all four datasets.
- ADE20K with
ignore_index=255: Keras and evaluate can now be compared, and all libraries agree (IoU 0.209 pooled, 0.335 per image). Forgettingignore_indexsilently lowers IoU to 0.206 in every library; only metric-diff's note points it out. - Translation (opus-mt on 300 WMT14 de-en sentences): BLEU 31.65 (sacrebleu) vs 0.275 in torchmetrics and nltk (whitespace tokenisation) and 0.317 in evaluate.
- Summarisation (16 systems from SummEval, 11 references each): ROUGE-Lsum 0.47 in torchmetrics vs 0.37 in rouge-score and evaluate. And evaluate's ROUGE is a bootstrap estimate, not the mean:
use_aggregator=Trueresamples the scores with numpy's unseeded global random generator, so it is slightly off and can change between runs (found on this data; now a detector). - Retrieval (BEIR SciFact, MiniLM and BM25): sklearn, torchmetrics, torcheval and trec_eval agree on NDCG, MAP, MRR, precision, recall and hit rate; only MAP@10 differs, by the known denominator choice (0.599 vs 0.596).
Usually none is "wrong": the libraries answer slightly different questions. Pick the convention your field reports and state it:
- Classification and regression: scikit-learn is the de facto reference; metric-diff uses it as the baseline. For multilabel data, say whether accuracy is subset or per label, and which average you used.
- Text generation: report BLEU and chrF from sacrebleu with its signature (tokenizer, smoothing, version), as recommended by Post 2018, and say whether ROUGE used stemming and one sentence per line.
- Ranking: trec_eval (pytrec_eval) is the reference behind TREC, MS MARCO and BEIR results; break score ties yourself and state how queries without relevant documents are handled.
- Segmentation: follow Metrics Reloaded (Maier-Hein et al., Nature Methods 2024), the consortium recommendations for choosing and aggregating image-analysis metrics, and report whether Dice/IoU is per image or pooled and whether background is included.
- Classification settings:
average(micro / macro / weighted, and samples for multilabel),threshold,pos_label,num_classes,zero_division,top_k(accuracy),exact_match(multilabel accuracy),weights(kappa). Not yet: classificationignore_index(masking the samples gives the same result in every library), sample weights, per-class output (average=None). - Multilabel covers accuracy, precision, recall, F1, AUROC and AP; MCC, kappa, balanced accuracy, specificity, Brier and log loss are binary and multiclass only. Regression has no settings yet (e.g.
multioutput). - ECE: equal-width bins and the L1 (expected) error only, as both libraries define it by default; netcal isn't compared (it pulls in pyro, gpytorch and tensorboard).
- Text: BLEU, chrF and ROUGE only (no METEOR, BERTScore, WER yet); nltk is compared on BLEU only (its chrF uses other definitions).
compare_batcheddoesn't cover text or ranking metrics. - Ranking: linear-gain NDCG only (as in sklearn, torchmetrics and trec_eval);
ranxisn't compared yet (it pulls in a large dependency tree). - Explanations describe the library versions tested (sklearn 1.9, torchmetrics 1.9, torcheval 0.0.7, Keras 3.15, evaluate 0.4.6, MONAI 1.6, sacrebleu 2.6, nltk 3.10, rouge-score 0.1.2, pytrec_eval 0.5, uncertainty-calibration 0.1.4). Tests check the key defaults so they fail loudly when a library changes.
- More settings: sample weights, per-class output; regression settings (
multioutput) - Detection mAP (pycocotools / COCO conventions)
- More edge cases in the matrix (send yours as an issue with
--jsonoutput) - Lightning and Keras callbacks that check the logged epoch metric
- WER/CER
Every known mismatch lives in src/metric_diff/cases.py (the matrix) and in tests/ as a regression case linked to its upstream issue. Found a new one? Open an issue with the --json output. Questions and ideas go to Discussions.
See CONTRIBUTING.md for how to add a detector or an edge case, and the Code of Conduct. Changes are listed in the changelog.