PyTorch torch.dist function
PyTorch torch reference manual
torch.dist is a PyTorch function for computing the distance between two tensors.
Function definition
torch.dist(input, other, p=2)
Parameter Description:
input- Input tensorother- Target tensorp- Norm type, defaults to 2 (Euclidean distance)
Usage examples
Example
import
torch
Create two tensors
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
y
=
torch.
tensor
(
[
4.0
,
5.0
,
6.0
]
)
Compute the Euclidean distance (p=2)
dist
=
torch.
dist
(
x
,
y
)
(
"Euclidean distance:"
,
dist
)
Compute the Manhattan distance (p=1)
dist_l1
=
torch.
dist
(
x
,
y
,
p
=
1
)
(
"Manhattan distance:"
,
dist_l1
)