PyTorch native metrics library providing 400+ rigorously tested metrics across classification, regression, audio, image, text, and other ML domains
Metrics for evaluating regression tasks including error measurements, correlation coefficients, and explained variance measures for continuous target prediction evaluation.
Measures various types of prediction errors for regression models.
class MeanSquaredError(Metric):
def __init__(
self,
squared: bool = True,
num_outputs: int = 1,
**kwargs
): ...
class MeanAbsoluteError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class MeanSquaredLogError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class LogCoshError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...Error metrics expressed as percentages of the true values.
class MeanAbsolutePercentageError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class SymmetricMeanAbsolutePercentageError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class WeightedMeanAbsolutePercentageError(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...Error metrics normalized by baseline measures.
class NormalizedRootMeanSquaredError(Metric):
def __init__(
self,
normalization: Union[str, Tensor] = "rmse",
num_outputs: int = 1,
**kwargs
): ...
class RelativeSquaredError(Metric):
def __init__(
self,
num_outputs: int = 1,
multioutput: str = "uniform_average",
**kwargs
): ...Measures of linear and monotonic relationships between predictions and targets.
class PearsonCorrCoef(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class SpearmanCorrCoef(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...
class KendallRankCorrCoef(Metric):
def __init__(
self,
variant: str = "b",
num_outputs: int = 1,
**kwargs
): ...
class ConcordanceCorrCoef(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...Measures the proportion of variance in the target variable explained by the model.
class R2Score(Metric):
def __init__(
self,
num_outputs: int = 1,
multioutput: str = "uniform_average",
adjusted: int = 0,
**kwargs
): ...
class ExplainedVariance(Metric):
def __init__(
self,
num_outputs: int = 1,
multioutput: str = "uniform_average",
**kwargs
): ...Measures of distance and similarity between prediction and target vectors.
class CosineSimilarity(Metric):
def __init__(
self,
reduction: str = "sum",
**kwargs
): ...
class MinkowskiDistance(Metric):
def __init__(
self,
p: float = 2.0,
**kwargs
): ...Information-theoretic measures of difference between prediction and target distributions.
class KLDivergence(Metric):
def __init__(
self,
reduction: str = "mean",
log_prob: bool = False,
**kwargs
): ...
class JensenShannonDivergence(Metric):
def __init__(
self,
reduction: str = "mean",
log_prob: bool = False,
**kwargs
): ...Domain-specific regression metrics for particular use cases.
class TweedieDevianceScore(Metric):
def __init__(
self,
power: float = 0.0,
**kwargs
): ...
class CriticalSuccessIndex(Metric):
def __init__(
self,
threshold: float = 0.0,
**kwargs
): ...
class ContinuousRankedProbabilityScore(Metric):
def __init__(
self,
num_outputs: int = 1,
**kwargs
): ...import torch
from torchmetrics import MeanSquaredError, MeanAbsoluteError, R2Score
# Initialize metrics
mse = MeanSquaredError()
mae = MeanAbsoluteError()
r2 = R2Score()
# Sample predictions and targets
preds = torch.randn(10, 1)
target = torch.randn(10, 1)
# Compute metrics
mse_score = mse(preds, target)
mae_score = mae(preds, target)
r2_score = r2(preds, target)
print(f"MSE: {mse_score:.4f}")
print(f"MAE: {mae_score:.4f}")
print(f"R²: {r2_score:.4f}")from torchmetrics import PearsonCorrCoef, SpearmanCorrCoef
# Initialize correlation metrics
pearson = PearsonCorrCoef()
spearman = SpearmanCorrCoef()
# Sample data
preds = torch.randn(100)
target = 2 * preds + torch.randn(100) * 0.1 # Linear relationship with noise
# Compute correlations
pearson_corr = pearson(preds, target)
spearman_corr = spearman(preds, target)
print(f"Pearson correlation: {pearson_corr:.4f}")
print(f"Spearman correlation: {spearman_corr:.4f}")from torchmetrics import MeanSquaredError, R2Score
# Multi-output regression
mse_multi = MeanSquaredError(num_outputs=3)
r2_multi = R2Score(num_outputs=3, multioutput="raw_values")
# Sample multi-output data
preds = torch.randn(50, 3)
target = torch.randn(50, 3)
# Compute metrics
mse_scores = mse_multi(preds, target)
r2_scores = r2_multi(preds, target)
print(f"MSE (overall): {mse_scores:.4f}")
print(f"R² per output: {r2_scores}")from torchmetrics import MeanAbsolutePercentageError, SymmetricMeanAbsolutePercentageError
# Initialize percentage error metrics
mape = MeanAbsolutePercentageError()
smape = SymmetricMeanAbsolutePercentageError()
# Sample data (avoid zeros in targets for MAPE)
preds = torch.randn(100) + 10
target = torch.randn(100) + 10
# Compute percentage errors
mape_score = mape(preds, target)
smape_score = smape(preds, target)
print(f"MAPE: {mape_score:.2f}%")
print(f"SMAPE: {smape_score:.2f}%")from torchmetrics import ContinuousRankedProbabilityScore
# CRPS for probabilistic forecasts
crps = ContinuousRankedProbabilityScore()
# Sample probabilistic predictions (ensemble of forecasts)
num_samples, num_forecasts = 100, 50
ensemble_preds = torch.randn(num_samples, num_forecasts)
target = torch.randn(num_samples)
# Compute CRPS
crps_score = crps(ensemble_preds, target)
print(f"CRPS: {crps_score:.4f}")MultioutputType = Union["raw_values", "uniform_average", "variance_weighted"]
ReductionType = Union["mean", "sum", "none"]Install with Tessl CLI
npx tessl i tessl/pypi-torchmetrics