I have a Tensor of shape NxN
which is basically the similarity or inner product of two tensors. I want to Get all the values which are above any threshold, say 0.5.
The result I’m looking for is something like: For each index i
, get all the values which are closer than 0.5
. Obviously for each index i
, there’ll be a minimum 1 element (self similarity is 1.0) and can be a maximum of N
elements (similarities
is NxN
matrix with the diagonal elements as 1
How could I go this?
x = torch.randn((9052, 512))
similarities = x @ x.T
scores, indices = torch.topk(similarities, x.shape[0]) # topk == shape[0] means get all the values, sorted
I have tried
mask = torch.ones(scores.size()[0])
mask = 1 - mask.diag()
sim_vec = torch.nonzero((scores >= 0.5)*mask)
Gives me a tensor of shape: torch.Size([51147416, 2])
I’ve also tried
(scores > 0.5 ).nonzero()
It too gives me a tensor of shape: torch.Size([51147416, 2])