YouTip LogoYouTip

Pytorch Torch Cdist

# PyTorch torch.cdist Function * * PyTorch torch Reference](#) `torch.cdist` is a PyTorch function for computing Euclidean distance matrices between two sets of points. It calculates the Euclidean distance between each point in the first input and each point in the second input, returning a distance matrix. ### Function Definition torch.cdist(input1, input2, p=2.0, compute_mode='use_mm_for_euclidean_dist') * * * ## Usage Example ## Example import torch # Calculate Euclidean distance between two sets of points x = torch.tensor([[0,0],[1,1],[2,2]]) # 3 2D points y = torch.tensor([[0,0],[1,0],[2,0]]) # 3 2D points # Distance matrix shape: (3, 3) distances = torch.cdist(x, y) print("Points x:") print(x) print("Points y:") print(y) print("Euclidean distance matrix:") print(distances) # Using different p values (Manhattan distance p=1) dist_l1 = torch.cdist(x, y, p=1.0) print("L1 distance (p=1):") print(dist_l1) # Using p=infinity (Chebyshev distance) dist_inf = torch.cdist(x, y, p=float('inf')) print("Chebyshev distance (p=inf):") print(dist_inf) * * PyTorch torch Reference](#) AI Processing... [](#)(#) [PyTorch torch.nn Reference](#)[](#) [ByteArk Coding Plan supports Doubao, GLM, DeepSeek, Kimi, MiniMax and other major models, officially supplied with stability and reliability. Configuration Guide Β₯9.9/month Subscribe Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E)
← Pytorch Torch Chain_MatmulPytorch Torch Baddbmm β†’