pytorch element intersection

Viewed 865

When I calculate the Hit Ratio, I need to calculate the number of elements of predict tensor in the target tensor, I wanna calculate the number of elements in their intersection.

For example: [# of classes: 20, # of samples: 2]

target: tensor([[14, 13, 8, 11, 18, 12, 5, 1, 0, 10], [ 8, 10, 2, 10, 7, 17, 6, 12, 13, 14]])

pred_idx: (HR@5): tensor([[14, 11, 8, 19, 4], [ 6, 9, 8, 13, 18]])

now when I do >>> (pred_idx & target).sum((1,2))

RuntimeError: The size of tensor a (5) must match the size of tensor b (10) at non-singleton dimension 1.

But the thing is, the prediction and target have different size, how can I calculate the number of elements in the pred@5 that are also in the target?

1 Answers

Perhaps you could convert to numpy and then use its set operations.

import torch
import numpy as np

target = torch.tensor([[14, 13, 8, 11, 18, 12, 5, 1, 0, 10], [ 8, 10, 2, 10, 7, 17, 6, 12, 13, 14]])
pred_idx = torch.tensor([[14, 11, 8, 19, 4], [ 6, 9, 8, 13, 18]])

Find elements of p@5 in target:

[np.intersect1d(t,p) for t,p in zip(target.cpu().numpy(),pred_idx.cpu().numpy())]

Find number of elements in p@5 also in target:

[len(np.intersect1d(t,p)) for t,p in zip(target.cpu().numpy(),pred_idx.cpu().numpy())]
Related