简体   繁体   English

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

[英]How does pytorch calculate matrix pairwise distance? Why isn't 'self' distance not zero?

If this is a naive question, please forgive me, my test code like this:如果这是一个幼稚的问题,请原谅我,我的测试代码是这样的:

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)

Result:结果:

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])

Questions are:问题是:

  1. How does pytorch calculate pairwise distance? pytorch 如何计算成对距离? Is it to calculate row vectors distance?是计算行向量距离吗?

  2. Why isn't 'self' distance 0?为什么“自我”距离不是 0?


Update更新

After changing list_1 and list_2 to this:将 list_1 和 list_2 更改为以下内容后:

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

Result becomes:结果变成:

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])

Looking at the documentation of nn.PairWiseDistance , pytorch expects two 2D tensors of N vectors in D dimensions, and computes the distances between the N pairs.查看nn.PairWiseDistance的文档,pytorch 需要D维中N向量的两个 2D 张量,并计算N对之间的距离。

Why "self" distance is not zero - probably because of floating point precision and because of eps = 1e-6 .为什么“自我”距离不为零 - 可能是因为浮点精度和因为eps = 1e-6

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算GPU上的成对距离矩阵 - How to calculate pairwise distance matrix on the GPU 如何在python中使用广播计算矩阵的成对余弦距离 - How to calculate pairwise cosine distance of matrix using broadcasting in python 在 Python 中计算加权成对距离矩阵 - Calculate weighted pairwise distance matrix in Python 如何计算坐标之间的成对harsine距离 - How to calculate the pairwise haversine distance between coordinates 如何将不对称的成对距离矩阵转换为字典? - How to convert an asymmetric pairwise distance matrix to dictionary? Python-如何生成成对汉明距离矩阵 - Python - How to generate the Pairwise Hamming Distance Matrix 如何计算 PyTorch 中点集和线之间的成对距离? - How to compute pairwise distance between point set and lines in PyTorch? numpy python:向量化距离函数以计算维度为 (m, 3) 的 2 个矩阵的成对距离 - numpy python: vectorize distance function to calculate pairwise distance of 2 matrix with a dimension of (m, 3) 如何计算集合之间的成对相似性(杰卡德距离) - how to calculate pairwise similarity (Jaccard Distance) between sets 如何使用自定义度量(设置距离)构造成对距离矩阵? - How can I construct a pairwise distance matrix using a custom metric (set distance)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM