Pytorch calculate hit ratio of predictions on batch level
So I would like to calculate my hit ratio at K samples for a recommendation problem I'm dealing with. For those who doesnt know hitratio at K means if the true sample class was in the top K classes predicted from the model. I would like to do this on a batch level.
So If I had the following code
import torch
import torch.nn as nn
import numpy as np

# 5 samples of 5 dimensions
yhat = torch.tensor([[4, 9, 7, 4, 0],
        [8, 1, 3, 1, 0],
        [9, 8, 4, 4, 8],
        [0, 9, 4, 7, 8],
        [8, 8, 0, 1, 4]])

# I get the top 3 classes of each sample recommendation
values, indices = torch.topk(a, 3)
# print(indices)
# tensor([[1, 2, 0],
#        [0, 2, 1],
#        [0, 1, 4],
#        [1, 4, 3],
#        [1, 0, 4]])

# True sample classes
ytrue = torch.tensor([2], #hits on top 3
                    [5],  #Doesnt hit on top 3
                    [4],  #hits on top 3
                    [0],  #Doesnt hit on top 3
                    [1])  #hits on top 3


I would like to calculate the hitratio at top 3 (HR3) here. HR3==1 Means all my predictions contained the true sample class in their top3 while 0 means none did.
For the given example I hit 3 out of 5. Result should be 3/5
What is the most efficient way on pytorch to do this?

Try:
(indices == ytrue.reshape(-1,1)).any(1)

Output:
tensor([ True, False,  True, False,  True])

If you want the ratio, convert the tensor to float and mean:
(indices == ytrue.reshape(-1,1)).any(1).float().mean()
# tensor(0.6000)

