minkowskidistance
Metric
MinkowskiDistancefromtorchmetrics(torchmetrics.MinkowskiDistance)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with MinkowskiDistance, or
mentions torchmetrics.MinkowskiDistance directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics import MinkowskiDistance
# MinkowskiDistance(p: float, **kwargs: Any) -> None
Library docstring
Compute `Minkowski Distance`_.
.. math::
d_{\text{Minkowski}} = \sum_{i}^N (| y_i - \hat{y_i} |^p)^\frac{1}{p}
where
:math: `y` is a tensor of target values,
:math: `\hat{y}` is a tensor of predictions,
:math: `\p` is a non-negative integer or floating-point number
This metric can be seen as generalized version of the standard euclidean distance which corresponds to minkowski
distance with p=2.
Args:
p: int or float larger than 1, exponent to which the difference between preds and target is to be raised
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
Example:
>>> from torchmetrics.regression import MinkowskiDistance
>>> target = tensor([1.0, 2.8, 3.5, 4.5])
>>> preds = tensor([6.1, 2.11, 3.1, 5.6])
>>> minkowski_distance = MinkowskiDistance(3)
>>> minkowski_distance(preds, target)
tensor(5.1220)
Quick recipe
import torchmetrics as _m
score = _m.MinkowskiDistance(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).