Assume that V is a Variable of size (10,6). I need to calculate the distances (e.g. the euclidean distance) between each row-vector contained in V, and store the 10-by-10 result into D. Is it possible to manage that operation without the “for” iterator?
At the expense of memory, you can do
((v.unsqueeze (0)-v.unsqueeze (1))**2).sum (2)**0.5
.
This works on master, on 0.1.12 you would need to throw in .expand (10,10,6)
after the unsqueeze
s.
Best regards
Thomas
1 Like
Would the following work? Would it use less memory?
TwoAB = 2 * A @ B.transpose(0,1)
print(torch.sqrt(torch.sum(A * A, 1).expand_as(TwoAB) + torch.sum(B * B, 1).transpose(0,1).expand_as(TwoAB) - TwoAB))
(here A
is one matrix, and B
is the other. In this thread’s case, they’d both be V
)
1 Like