rust_metrics
rust_metrics is an ML evaluation toolkit that brings Torchmetrics-style metrics to Rust. Each metric implements the same incremental
Metric trait, so you can feed batched predictions over time and ask for the final score when ready.
Every metric shares the same Torch-inspired test cases and examples so that the
crate docs mirror the upstream behavior where the functionality matches.
Some benefits of rust-metrics:
- A standardized interface to increase reproducibility
- Reduces Boilerplate
- Rigorously tested
- Automatic accumulation over batches
Getting started
Add the crate to your project:
# or enable the BERT-based similarity metric
TorchMetrics-aligned examples
All snippets below (and method examples) reuse the exact inputs from the public TorchMetrics docs so you can cross-check the expected values.
Classification
use ;
let target = ;
let preds = ;
let mut acc = default;
acc.update.unwrap;
assert!;
let mut auroc = new;
let auroc_scores = ;
let auroc_target = ;
auroc.update.unwrap;
assert!;
Regression
use ;
let mut mse = default;
mse.update.unwrap;
assert!;
let mut mae = default;
mae.update.unwrap;
assert!;
Text
use ;
let preds = ;
let targets = ;
let mut bleu = default;
bleu.update.unwrap;
assert!;
let mut edit = default;
edit.update.unwrap;
assert_eq!;
For SentenceEmbeddingSimilarity enable the text-bert feature; it mirrors the BERTScore example sentences and
reports cosine similarities for each pair instead of precision/recall triples.
Implemented metrics
Classification
BinaryAccuracy,MulticlassAccuracyBinaryPrecision,BinaryRecall,MulticlassPrecisionBinaryF1Score,MulticlassF1ScoreBinaryHingeLoss,MulticlassHingeLossBinaryJaccardIndex,MulticlassJaccardIndexBinaryConfusionMatrixBinaryAuroc
Regression
MeanSquaredErrorNormalizedRootMeanSquaredErrorMeanAbsoluteErrorMeanAbsolutePercentageErrorR2Score
Text
Bleuwith optional smoothing and arbitrary n-gram depthEditDistancewith sum or mean reductionRougeScoreSentenceEmbeddingSimilarity(requires thetext-bertfeature) backed byfastembed. This metric embeds each sentence pair with lightweight BERT embeddings and reports cosine similarity scores.
Feature flags
| Feature | Default | Description |
|---|---|---|
text-bert |
no | Enables BERT sentence embedding similarity via fastembed. |