I want to calculate the l2 distance among all elements of two vector. naive version code is:
import torch
n = 4
d = 2
A = torch.randn(n,d)
B = torch.randn(n,d)
C = torch.zeros(n,n)
for i in range(n):
for j in range(n):
C[i][j] = torch.sum(torch.sqrt((A[i] - B[j])*(A[i]-B[j])))
I want to ask is there any easier way to do this calculation at PyTorch?
Thanks!
Jun
In [3]: a = torch.autograd.Variable(torch.rand(3,4), requires_grad=True)
In [4]: b = torch.autograd.Variable(torch.rand(4,2), requires_grad=True)
In [5]: c = a @ b
In [6]: c
Out[6]:
Variable containing:
0.9375 0.5729
1.3275 0.4193
0.2320 0.1663
[torch.FloatTensor of size 3x2]
Cool, but it’s only the gram matrix, and I want to calculate the Euclidean Distance Matrix here (sorry, I did not explain it well before). Is there any better way?