0
votes

Have 2 batches of vectors one matrix is 128 X 120 and the other is 100 X 120 how do I computer euclidean distance?

So I have 128 vectors of length 120 and another 100 vectors of length 120. I want to compute the Euclidean distance between the 128 with each of the 100 vectors in the other tensor. So my return result should be 128 X 100, as in the first 128 vector's euclidean distance for each of the 100 vectors in the other batch where tensor[0,0] corresponds to the Euclidean distance between vector 0 out of 128 and vector 0 out of 100 and tensor[0,-1] corresponds to the euclidean distance between vector 0 out of the 128 and the last vector out of the 100 in the other batch.

I'm sure there's a way to do this without a double for loop but I'm not sure how.

1

1 Answers

0
votes

You have x of shape 128x120 and y of shape 100x120.
The naive way to compute L2 distances would be:

dist = torch.sqrt(((x[:, None, :] - y[None, ...])**2).sum(dim=2))

However, this method has a memory footprint of 128x100x120.

A more efficient way (taking advantage of the relation between matrix product and the L2 distance) would be:

yt = y.T
dist = torch.sqrt((x**2).sum(dim=1,keepdim=True) + (yt**2).sum(dim=0,keepdim=True) - 2*x @ yt)