【问题标题】:How does pytorch calculate matrix pairwise distance? Why isn't 'self' distance not zero?pytorch如何计算矩阵成对距离?为什么“自我”距离不为零?
【发布时间】:2018-12-04 07:25:34
【问题描述】:

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

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)

结果:

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

问题是:

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

  2. 为什么'self'距离不是0?


更新

将 list_1 和 list_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])

【问题讨论】:

    标签: python pytorch pairwise-distance


    【解决方案1】:

    查看nn.PairWiseDistance 的文档,pytorch 期望D 维度中的N 向量的两个二维张量,并计算N 对之间的距离。

    为什么“自我”距离不为零——可能是因为floating point precisioneps = 1e-6

    【讨论】:

    • ,谢谢,我想知道它是如何计算的?我更新了问题
    【解决方案2】:

    根据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.
    

    【讨论】:

    • 是的,但是如何进行计算?
    • @Shai,对不起,我说的不够清楚,当我说“计算”时,我想知道对什么样的元素进行什么样的计算,以及“计算p范数输入中每对行向量之间的距离”告诉我,p-范数距离将在对应的行向量上计算。
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多