forked from catalyst-team/catalyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_hitrate.py
52 lines (41 loc) · 1.72 KB
/
_hitrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import List
import torch
from catalyst.metrics.functional._misc import process_recsys_components
def hitrate(outputs: torch.Tensor, targets: torch.Tensor, topk: List[int]) -> List[torch.Tensor]:
"""
Calculate the hit rate (aka recall) score given
model outputs and targets.
Hit-rate is a metric for evaluating ranking systems.
Generate top-N recommendations and if one of the recommendation is
actually what user has rated, you consider that a hit.
By rate we mean any explicit form of user's interactions.
Add up all of the hits for all users and then divide by number of users
Compute top-N recommendation for each user in the training stage
and intentionally remove one of this items fro the training data.
Args:
outputs (torch.Tensor):
Tensor with predicted score
size: [batch_size, slate_length]
model outputs, logits
targets (torch.Tensor):
Binary tensor with ground truth.
1 means the item is relevant
for the user and 0 not relevant
size: [batch_size, slate_length]
ground truth, labels
topk (List[int]):
Parameter fro evaluation on top-k items
Returns:
hitrate_at_k (List[torch.Tensor]): the hitrate score
"""
results = []
targets_sort_by_outputs = process_recsys_components(outputs, targets)
for k in topk:
k = min(outputs.size(1), k)
if targets.sum(dim=1).data[0] != 0:
hits_score = torch.sum(targets_sort_by_outputs[:, :k], dim=1) / targets.sum(dim=1)
else:
hits_score = torch.tensor([0.0])
results.append(torch.mean(hits_score))
return results
__all__ = ["hitrate"]