-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
61 lines (48 loc) · 1.65 KB
/
metric.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
53
54
55
56
57
58
59
60
61
import torch
def get_recall(indices, targets):
"""
Calculates the recall score for the given predictions and targets
Args:
indices (Bxk): torch.LongTensor. top-k indices predicted by the model.
targets (B): torch.LongTensor. actual target indices.
Returns:
recall (float): the recall score
"""
targets = targets.view(-1, 1).expand_as(indices)
hits = (targets == indices).nonzero()
if len(hits) == 0:
return 0
n_hits = (targets == indices).nonzero()[:, :-1].size(0)
recall = float(n_hits) / targets.size(0)
return recall
def get_mrr(indices, targets):
"""
Calculates the MRR score for the given predictions and targets
Args:
indices (Bxk): torch.LongTensor. top-k indices predicted by the model.
targets (B): torch.LongTensor. actual target indices.
Returns:
mrr (float): the mrr score
"""
tmp = targets.view(-1, 1)
targets = tmp.expand_as(indices)
hits = (targets == indices).nonzero()
ranks = hits[:, -1] + 1
ranks = ranks.float()
rranks = torch.reciprocal(ranks)
mrr = torch.sum(rranks).data / targets.size(0)
return mrr.item()
def evaluate(indices, targets, k=20):
"""
Evaluates the model using Recall@K, MRR@K scores.
Args:
logits (B,C): torch.LongTensor. The predicted logit for the next items.
targets (B): torch.LongTensor. actual target indices.
Returns:
recall (float): the recall score
mrr (float): the mrr score
"""
_, indices = torch.topk(indices, k, -1)
recall = get_recall(indices, targets)
mrr = get_mrr(indices, targets)
return recall, mrr