-
Notifications
You must be signed in to change notification settings - Fork 24
/
util.py
55 lines (50 loc) · 2.08 KB
/
util.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
from typing import Dict, Tuple, Any
from warmup_scheduler import GradualWarmupScheduler
def global_average_precision_score(
y_true: Dict[Any, Any],
y_pred: Dict[Any, Tuple[Any, float]]
) -> float:
"""
Compute Global Average Precision score (GAP)
Parameters
----------
y_true : Dict[Any, Any]
Dictionary with query ids and true ids for query samples
y_pred : Dict[Any, Tuple[Any, float]]
Dictionary with query ids and predictions (predicted id, confidence
level)
Returns
-------
float
GAP score
"""
indexes = list(y_pred.keys())
indexes.sort(
key=lambda x: -y_pred[x][1],
)
queries_with_target = len([i for i in y_true.values() if i is not None])
correct_predictions = 0
total_score = 0.
for i, k in enumerate(indexes, 1):
relevance_of_prediction_i = 0
if y_true[k] == y_pred[k][0]:
correct_predictions += 1
relevance_of_prediction_i = 1
precision_at_rank_i = correct_predictions / i
total_score += precision_at_rank_i * relevance_of_prediction_i
return 1 / queries_with_target * total_score
class GradualWarmupSchedulerV2(GradualWarmupScheduler):
def __init__(self, optimizer, multiplier, total_epoch, after_scheduler=None):
super(GradualWarmupSchedulerV2, self).__init__(optimizer, multiplier, total_epoch, after_scheduler)
def get_lr(self):
if self.last_epoch > self.total_epoch:
if self.after_scheduler:
if not self.finished:
self.after_scheduler.base_lrs = [base_lr * self.multiplier for base_lr in self.base_lrs]
self.finished = True
return self.after_scheduler.get_lr()
return [base_lr * self.multiplier for base_lr in self.base_lrs]
if self.multiplier == 1.0:
return [base_lr * (float(self.last_epoch) / self.total_epoch) for base_lr in self.base_lrs]
else:
return [base_lr * ((self.multiplier - 1.) * self.last_epoch / self.total_epoch + 1.) for base_lr in self.base_lrs]