PyTorch torch.cdist 関数
torch.cdistは、PyTorch で2つの点群間のユークリッド距離行列を計算するための関数です。最初の入力の各点と2番目の入力の各点との間のユークリッド距離を計算し、距離行列を返します。
関数定義
torch.cdist(input1, input2, p=2.0, compute_mode='use_mm_for_euclidean_dist')
使用例
例
import torch
# 2つの点群間のユークリッド距離を計算
x = torch.tensor([[0, 0], [1, 1], [2, 2]]) # 3 つの 2D 点
y = torch.tensor([[0, 0], [1, 0], [2, 0]]) # 3 つの 2D 点
# 距離行列の形状: (3, 3)
distances = torch.cdist(x, y)
print("点 x:")
print(x)
print("点 y:")
print(y)
print("ユークリッド距離行列:")
print(distances)
# 異なる p 値を使用(マンハッタン距離 p=1)
dist_l1 = torch.cdist(x, y, p=1.0)
print("L1 距離 (p=1):")
print(dist_l1)
# p=無限大(チェビシェフ距離)を使用
dist_inf = torch.cdist(x, y, p=float('inf'))
print("チェビシェフ距離 (p=inf):")
print(dist_inf)
# 2つの点群間のユークリッド距離を計算
x = torch.tensor([[0, 0], [1, 1], [2, 2]]) # 3 つの 2D 点
y = torch.tensor([[0, 0], [1, 0], [2, 0]]) # 3 つの 2D 点
# 距離行列の形状: (3, 3)
distances = torch.cdist(x, y)
print("点 x:")
print(x)
print("点 y:")
print(y)
print("ユークリッド距離行列:")
print(distances)
# 異なる p 値を使用(マンハッタン距離 p=1)
dist_l1 = torch.cdist(x, y, p=1.0)
print("L1 距離 (p=1):")
print(dist_l1)
# p=無限大(チェビシェフ距離)を使用
dist_inf = torch.cdist(x, y, p=float('inf'))
print("チェビシェフ距離 (p=inf):")
print(dist_inf)
その他の拡張機能
Pytorch torch リファレンスマニュアル