6

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? Is it to calculate row vectors distance?

  2. Why isn't 'self' distance 0?


Update

After changing list_1 and list_2 to this:

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])
Shikkediel
  • 5,195
  • 16
  • 45
  • 77
Alex Luya
  • 9,412
  • 15
  • 59
  • 91

2 Answers2

5

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.

Why "self" distance is not zero - probably because of floating point precision and because of eps = 1e-6.

Shai
  • 111,146
  • 38
  • 238
  • 371
0

according to 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.
Alex Luya
  • 9,412
  • 15
  • 59
  • 91
  • yes, but **how** is the calculation is carried out? – Shai Dec 04 '18 at 12:58
  • @Shai,sorry,I didn't get it clear enough,when I say "calculation",I want to know what kinds of calculation on what kind of elments,and "Computes the p-norm distance between every pair of row vectors in the input" told me that the p-norm distance will be calculated on corresponded row vectors. – Alex Luya Dec 05 '18 at 02:26