PyTorch torch.dist Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.distIt is a function in PyTorch used to compute the distance between two tensors.

Function Definition

torch.dist(input, other, p=2)

Parameter Description:

  • input- Input tensor
  • other- Target tensor
  • p- Norm type, defaults to 2 (Euclidean distance)

Usage Example

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 Euclidean distance (p=2)
dist = torch.dist(x, y)
print("Euclidean distance:", dist)

# Compute Manhattan distance (p=1)
dist_l1 = torch.dist(x, y, p=1)
print("Manhattan distance:", dist_l1)

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions