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 (
NxN matrix with the diagonal elements as
How could I go this?
x = torch.randn((9052, 512)) similarities = x @ x.T scores, indices = torch.topk(similarities, x.shape) # topk == shape means get all the values, sorted
I have tried
mask = torch.ones(scores.size()) mask = 1 - mask.diag() sim_vec = torch.nonzero((scores >= 0.5)*mask)
Gives me a tensor of shape:
I’ve also tried
(scores > 0.5 ).nonzero()
It too gives me a tensor of shape: