-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathevaluate.py
More file actions
executable file
·101 lines (78 loc) · 2.72 KB
/
Copy pathevaluate.py
File metadata and controls
executable file
·101 lines (78 loc) · 2.72 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
import math
def get_eval_metrics_results(predictions, labels):
# predictions = [_.strip().replace(" ","") for _ in predictions]
# labels = [_.strip().replace(" ","") for _ in labels]
predictions = [str(pred[1:5]) for pred in predictions]
labels = [str(label[:4]) for label in labels]
results = []
for i in range(len(labels)):
pred = predictions[i]
label = labels[i]
one_results = []
if pred == label:
one_results.append(1)
else:
one_results.append(0)
results.append(one_results)
metrics_results = get_metrics_results(results, metrics=['hit@1'])
metric = dict()
for k, v in metrics_results.items():
metric[k.replace('@', '_at_')] = v / len(labels)
return metric
def get_topk_results(predictions, scores, targets, k, all_items=None):
results = []
B = len(targets)
# predictions = [_.split("Response:")[-1] for _ in predictions]
predictions = [_.strip().replace(" ","") for _ in predictions]
# print(predictions)##################
if all_items is not None:
for i, seq in enumerate(predictions):
if seq not in all_items:
scores[i] = -1000
# print(scores)
for b in range(B):
batch_seqs = predictions[b * k: (b + 1) * k]
batch_scores = scores[b * k: (b + 1) * k]
pairs = [(a, b) for a, b in zip(batch_seqs, batch_scores)]
# print(pairs)
sorted_pairs = sorted(pairs, key=lambda x: x[1], reverse=True)
target_item = targets[b]
one_results = []
for sorted_pred in sorted_pairs:
if sorted_pred[0] == target_item:
one_results.append(1)
else:
one_results.append(0)
results.append(one_results)
return results
def get_metrics_results(topk_results, metrics):
res = {}
for m in metrics:
if m.lower().startswith("hit"):
k = int(m.split("@")[1])
res[m] = hit_k(topk_results, k)
elif m.lower().startswith("ndcg"):
k = int(m.split("@")[1])
res[m] = ndcg_k(topk_results, k)
else:
raise NotImplementedError
return res
def ndcg_k(topk_results, k):
"""
Since we apply leave-one-out, each user only have one ground truth item, so the idcg would be 1.0
"""
ndcg = 0.0
for row in topk_results:
res = row[:k]
one_ndcg = 0.0
for i in range(len(res)):
one_ndcg += res[i] / math.log(i + 2, 2)
ndcg += one_ndcg
return ndcg
def hit_k(topk_results, k):
hit = 0.0
for row in topk_results:
res = row[:k]
if sum(res) > 0:
hit += 1
return hit