-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
42 lines (34 loc) · 976 Bytes
/
utils.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
import numpy as np
import torch
import gc
def pfbeta(labels, predictions, beta=1.0):
y_true_count = 0
ctp = 0
cfp = 0
for idx in range(len(labels)):
prediction = min(max(predictions[idx], 0), 1)
if labels[idx]:
y_true_count += 1
ctp += prediction
else:
cfp += prediction
beta_squared = beta * beta
c_precision = ctp / (ctp + cfp)
c_recall = ctp / max(y_true_count, 1) # avoid / 0
if c_precision > 0 and c_recall > 0:
result = (
(1 + beta_squared)
* (c_precision * c_recall)
/ (beta_squared * c_precision + c_recall)
)
return result
else:
return 0
def optimal_f1(labels, predictions):
thres = np.linspace(0, 1, 101)
f1s = [pfbeta(labels, predictions > thr) for thr in thres]
idx = np.argmax(f1s)
return f1s[idx], thres[idx]
def gc_collect():
gc.collect()
torch.cuda.empty_cache()