Skip to content

Asymmetric euclidean distance pairwise calculation #15

Open

Description

@LemonPi

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions