visinf / n3net

Neural Nearest Neighbors Networks (NIPS*2018)
Other
283 stars 45 forks source link

Asymmetric euclidean distance pairwise calculation #15

Open LemonPi opened 5 years ago

LemonPi commented 5 years ago

The current euclidean distance calculation produces non-symmetric distance matrices. I suspect this is a numeric issue (although it persists even when using float64):

    out = -2 * torch.matmul(x, y)
    out += (x ** 2).sum(dim=-1, keepdim=True)
    out += (y ** 2).sum(dim=-2, keepdim=True)
    a = out.t() - out
tensor([[ 0.0000e+00,  0.0000e+00,  0.0000e+00,  ..., -7.1054e-15,
         -7.1054e-15,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  ...,  7.1054e-15,
          0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  ..., -7.1054e-15,
          0.0000e+00, -7.1054e-15],
        ...,
        [ 7.1054e-15, -7.1054e-15,  7.1054e-15,  ...,  0.0000e+00,
          0.0000e+00,  0.0000e+00],
        [ 7.1054e-15,  0.0000e+00,  0.0000e+00,  ...,  0.0000e+00,
          0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  7.1054e-15,  ...,  0.0000e+00,
          0.0000e+00,  0.0000e+00]], dtype=torch.float64)

Apparently sklearn.metric.pairwise_distance also has this issue...