Compute Squared distance b/w two tensors inside a model in PyTorch

Compute Squared distance b/w two tensors inside a model in PyTorch :

D = | P1− P2 | ^ 2

options:

    torch.norm(p1 - p2, dim=0)
    (p1 - p2).pow(2).sum(1)
    torch.dist(p1, p2, 2)

which one is correct for just the squared distance between two tensors?

Hi,

  • The first one computes the 2 norm, so you should square it if you want a squared norm.
  • The second one computes what you want but also sums over dimension 1.
  • The last one also computes what you want but does not do any reduction.
1 Like