-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
155 lines (131 loc) · 4.36 KB
/
metrics.py
File metadata and controls
155 lines (131 loc) · 4.36 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import numpy as np
import torch
import math
from sklearn.metrics import roc_auc_score
from constants import DEVICE
def get_recommendation_metrics(logits, labels):
"""
This method calculates the following metrics for the given logits and labels
1. MRR - Mean Reciprocal Rank
2. Hits@1
3. Hits@3
4. Hits@5
5. Hits@10
"""
## Check if logits and labels are numpy arrays
if not isinstance(logits, np.ndarray):
# print(logits.shape, labels.shape, labels)
logits = logits.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
mrr = 0
hits_at_1 = 0
hits_at_3 = 0
hits_at_5 = 0
hits_at_10 = 0
for i in range(len(logits)):
logit = logits[i]
label = labels[i]
sorted_indices = np.argsort(logit)[::-1]
rank = np.where(sorted_indices == label)[0][0] + 1
mrr += 1/rank
if rank == 1:
hits_at_1 += 1
if rank <= 3:
hits_at_3 += 1
if rank <= 5:
hits_at_5 += 1
if rank <= 10:
hits_at_10 += 1
mrr /= len(logits)
hits_at_1 /= len(logits)
hits_at_3 /= len(logits)
hits_at_5 /= len(logits)
hits_at_10 /= len(logits)
return {
'MRR': mrr,
'Hits@1': hits_at_1,
'Hits@3': hits_at_3,
'Hits@5': hits_at_5,
'Hits@10': hits_at_10,
}
def get_recommendation_metrics_multi_label(logits, labels):
"""
This method calculates the following metrics for the given logits and labels
where labels is a n x k matrix of 0s and 1s where n is the number of samples and k is the number of classes
logits allows for multiple classes to be predicted for each sample
1. MRR - Mean Reciprocal Rank
2. Hits@1
3. Hits@3
4. Hits@5
5. Hits@10
"""
if not isinstance(logits, np.ndarray):
logits = logits.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
mrr = 0
hits_at_1 = 0
hits_at_3 = 0
hits_at_5 = 0
hits_at_10 = 0
for i in range(len(logits)):
logit = logits[i]
allowed_labels = np.where(labels[i] == 1)[0]
b_rank, b_hits_at_1, b_hits_at_3, b_hits_at_5, b_hits_at_10 = logit.shape[-1], 0, 0, 0, 0
for label in allowed_labels:
sorted_indices = np.argsort(logit)[::-1]
rank = np.where(sorted_indices == label)[0][0] + 1
b_rank = min(b_rank, rank)
if rank == 1:
b_hits_at_1 = 1
if rank <= 3:
b_hits_at_3 = 1
if rank <= 5:
b_hits_at_5 = 1
if rank <= 10:
b_hits_at_10 = 1
mrr += 1/b_rank
if rank == 1:
hits_at_1 += b_hits_at_1
if rank <= 3:
hits_at_3 += b_hits_at_3
if rank <= 5:
hits_at_5 += b_hits_at_5
if rank <= 10:
hits_at_10 += b_hits_at_10
mrr /= len(logits)
hits_at_1 /= len(logits)
hits_at_3 /= len(logits)
hits_at_5 /= len(logits)
hits_at_10 /= len(logits)
return {
'MRR': mrr,
'Hits@1': hits_at_1,
'Hits@3': hits_at_3,
'Hits@5': hits_at_5,
'Hits@10': hits_at_10,
}
def compute_auc(pos_score, neg_score):
"""
This method computes the AUC score for the given positive and negative scores
"""
scores = torch.cat([pos_score, neg_score]).cpu().detach().numpy()
labels = torch.cat(
[torch.ones(pos_score.shape[0]), torch.zeros(neg_score.shape[0])]).cpu().detach().numpy()
return roc_auc_score(labels, scores)
def compute_loss(pos_score, neg_score):
scores = torch.cat([pos_score, neg_score]).to(DEVICE)
labels = torch.cat([torch.ones(pos_score.shape[0]), torch.zeros(neg_score.shape[0])]).to(DEVICE)
return torch.nn.BCEWithLogitsLoss()(scores.float(), labels.float())
def get_prelexity(loss):
"""
This method computes the evaluation stats for the given eval result
"""
return math.exp(loss)
def compute_metrics(eval_preds):
"""
This method computes the metrics for the given eval preds
This method is used as a callback in the Trainer class of the transformers library
"""
logits, labels = eval_preds
recommendation_metrics = get_recommendation_metrics(logits, labels)
return recommendation_metrics