-
Notifications
You must be signed in to change notification settings - Fork 10
/
eval.py
39 lines (31 loc) · 1.43 KB
/
eval.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
import math
import torch
import numpy as np
from sklearn.metrics import average_precision_score
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score
TEST_BATCH_SIZE = 32
def eval_one_epoch(model, sampler, src, dst, ts, label, val_e_idx_l=None):
val_ap, val_auc = [], []
with torch.no_grad():
tgan = model.eval()
num_test_instance = len(src)
num_test_batch = math.ceil(num_test_instance / TEST_BATCH_SIZE)
for k in range(num_test_batch):
s_idx = k * TEST_BATCH_SIZE
e_idx = min(num_test_instance - 1, s_idx + TEST_BATCH_SIZE)
if s_idx == e_idx:
continue
src_l_cut = src[s_idx:e_idx]
dst_l_cut = dst[s_idx:e_idx]
ts_l_cut = ts[s_idx:e_idx]
e_l_cut = val_e_idx_l[s_idx:e_idx] if (val_e_idx_l is not None) else None
label_l_cut = label[s_idx:e_idx]
size = len(src_l_cut)
_, dst_l_fake = sampler.sample(size)
pos_prob, neg_prob = tgan.inference(src_l_cut, dst_l_cut, dst_l_fake, ts_l_cut, e_l_cut)
pred_score = np.concatenate([(pos_prob).cpu().numpy(), (neg_prob).cpu().numpy()])
true_label = np.concatenate([np.ones(size), np.zeros(size)])
val_ap.append(average_precision_score(true_label, pred_score))
val_auc.append(roc_auc_score(true_label, pred_score))
return np.mean(val_ap), np.mean(val_auc)