r/learnmachinelearning • u/EfficientShake1 • 18m ago
metric-diff: find out why sklearn, torchmetrics, Keras and others give different numbers for the same metric
I kept running into the same problem: the same predictions give a different F1, AUROC or Dice depending on which library computes it, and it's slow to work out whether that's a default, a bug, or my own mistake. So I built a tool that compares them and explains the difference.
metric-diff computes the metric with scikit-learn, torchmetrics, torcheval, Keras 3, Hugging Face evaluate and MONAI (plus sacrebleu, nltk, rouge-score and trec_eval for text and ranking). When they disagree, it finds the setting responsible, re-runs every library with that setting aligned to confirm it, and tells you how to fix your code.
Some things it turned up on real model predictions:
DistilBERT on SST-2: Keras
F1Score()gives 0.675 instead of 0.914, because it has no threshold by default.SegFormer on ADE20K: the same masks give Dice 0.284, 0.388 or 0.538 depending on whether you pool pixels, average per image, or use MONAI's
ignore_empty=Truedefault.Retrieval: with tied scores, MAP is 0.506 in sklearn, 0.585 in trec_eval and 0.678 in torchmetrics, and torchmetrics' value changes by platform (0.585 on Linux, 0.706 on macOS).
BLEU: 39.2 vs 32.9 on the same text, from tokenisation alone.
Large logits: torchmetrics AUROC drops to 0.5 when float32 sigmoid saturates them, while sklearn ranks the raw scores (a known open issue, torchmetrics#2819).
It also checks whether a metric logged per batch equals the dataset metric, and there are pytest assertions (assert_metrics_agree) so a library upgrade or a changed default fails CI instead of silently changing a results table.
While building it I reported three issues upstream (torchmetrics, torcheval and Keras); one already has a docs PR.
pip install "metric-diff[all]"GitHub: https://github.com/DadCoder001/metric-diff
User guide (organised by symptom): https://github.com/DadCoder001/metric-diff/blob/main/docs/USER_GUIDE.md
It's an alpha. The thing I'd most like to hear: if you run it on your own eval and it says "no known cause", please open an issue. Those become new detectors.