-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmonitor.py
44 lines (32 loc) · 1.33 KB
/
monitor.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
from blocks.monitoring.aggregation import MonitoredQuantity
from sklearn import metrics
class FScoreQuantity(MonitoredQuantity):
def __init__(self, average='macro', threshold=0.5, **kwargs):
self.average = average
self.threshold = threshold
super(FScoreQuantity, self).__init__(**kwargs)
def initialize(self):
self.total_f_score, self.examples_seen = 0.0, 0
def aggregate(self, y, y_hat):
self.total_f_score += metrics.f1_score(y, y_hat > self.threshold,
average=self.average)
self.examples_seen += 1
def get_aggregated_value(self):
res = self.total_f_score / self.examples_seen
return res
class AUCQuantity(MonitoredQuantity):
def __init__(self, average='macro', **kwargs):
self.average = average
super(AUCQuantity, self).__init__(**kwargs)
def initialize(self):
self.total_auc_score, self.examples_seen = 0.0, 0
def aggregate(self, y, y_hat):
try:
self.total_auc_score += metrics.roc_auc_score(y, y_hat,
average=self.average)
except:
pass
self.examples_seen += 1
def get_aggregated_value(self):
res = self.total_auc_score / self.examples_seen
return res