# 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:

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:
Return type:
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()

>>> 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)


## 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:
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)