Minkowski Distance

Module Interface

class torchmetrics.MinkowskiDistance(p, **kwargs)[source]

Compute Minkowski Distance.

\[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.

Parameters:
  • p (float) – int or float larger than 1, exponent to which the difference between preds and target is to be raised

  • kwargs (Any) – Additional keyword arguments, see Advanced metric settings 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)
plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
  • val (Union[Tensor, Sequence[Tensor], None]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.

  • ax (Optional[Axes]) – An matplotlib axis object. If provided will add plot to that axis

Return type:

tuple[Figure, Union[Axes, ndarray]]

Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> from torch import randn
>>> # Example plotting a single value
>>> from torchmetrics.regression import MinkowskiDistance
>>> metric = MinkowskiDistance(p=3)
>>> metric.update(randn(10,), randn(10,))
>>> fig_, ax_ = metric.plot()
../_images/minkowski_distance-1.png
>>> from torch import randn
>>> # Example plotting multiple values
>>> from torchmetrics.regression import MinkowskiDistance
>>> metric = MinkowskiDistance(p=3)
>>> values = []
>>> for _ in range(10):
...     values.append(metric(randn(10,), randn(10,)))
>>> fig, ax = metric.plot(values)
../_images/minkowski_distance-2.png

Functional Interface

torchmetrics.functional.minkowski_distance(preds, targets, p)[source]

Compute the Minkowski distance.

\[\begin{split}d_{\text{Minkowski}} = \\sum_{i}^N (| y_i - \\hat{y_i} |^p)^\frac{1}{p}\end{split}\]

This metric can be seen as generalized version of the standard euclidean distance which corresponds to minkowski distance with p=2.

Parameters:
  • preds (Tensor) – estimated labels of type Tensor

  • targets (Tensor) – ground truth labels of type Tensor

  • p (float) – int or float larger than 1, exponent to which the difference between preds and target is to be raised

Return type:

Tensor

Returns:

Tensor with the Minkowski distance

Example

>>> from torchmetrics.functional.regression import minkowski_distance
>>> x = torch.tensor([1.0, 2.8, 3.5, 4.5])
>>> y = torch.tensor([6.1, 2.11, 3.1, 5.6])
>>> minkowski_distance(x, y, p=3)
tensor(5.1220)