Pythorch如何计算矩阵对距离?为什么“自我”距离不是零?

2024-09-30 04:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

如果这是一个幼稚的问题,请原谅我,我的测试代码是这样的:

import torch
from torch.nn.modules.distance import PairwiseDistance

list_1 = [[1., 1.,],[1., 1.]]
list_2 = [[1., 1.,],[2., 1.]]

mtrxA=torch.tensor(list_1)
mtrxB=torch.tensor(list_2)

print "A-B distance     :",PairwiseDistance(2).forward(mtrxA, mtrxB)
print "A 'self' distance:",PairwiseDistance(2).forward(mtrxA, mtrxA)
print "B 'self' distance:",PairwiseDistance(2).forward(mtrxB, mtrxB)

结果:

^{pr2}$

问题是:

  1. Pythorch如何计算成对距离?是为了计算行向量距离吗?

  2. 为什么“自我”距离不是0?


更新

将列表“1”和“列表2”更改为:

list_1 = [[1., 1.,1.,],[1., 1.,1.,]]
list_2 = [[1., 1.,1.,],[2., 1.,1.,]]

结果变成:

A-B distance     : tensor([1.7321e-06, 1.0000e+00])
A 'self' distance: tensor([1.7321e-06, 1.7321e-06])
B 'self' distance: tensor([1.7321e-06, 1.7321e-06])

Tags: fromimportself距离列表torchlistdistance
2条回答

看一下^{}的文档,Pythorch期望在D维中有两个N向量的二维张量,并计算N对之间的距离。在

为什么“自我”距离不是零-可能是因为floating point precision和{}。在

根据https://github.com/pytorch/pytorch/blob/master/torch/nn/functional.py

Computes the p-norm distance between every pair of row vectors in the input.

相关问题 更多 >

    热门问题