-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
57 lines (44 loc) · 1.82 KB
/
data_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from sklearn.metrics import roc_auc_score
import torch.nn.functional as F
import numpy as np
import torch
def eval_acc(targ, prob):
# generalized version for both single/multi-label classification
pred = prob.max(dim=-1)[1].type_as(targ)
acc = pred.eq(targ.squeeze(dim=-1)).double().sum() / targ.numel()
acc = acc.item()
return acc
def eval_rocauc(y_true, y_pred):
""" adapted from ogb
https://github.com/snap-stanford/ogb/blob/master/ogb/nodeproppred/evaluate.py"""
rocauc_list = []
y_true = y_true.detach().cpu().numpy()
if y_true.shape[1] == 1:
# use the predicted class for single-class classification
y_pred = F.softmax(y_pred, dim=-1)[:, 1].unsqueeze(1).detach().cpu().numpy()
else:
y_pred = y_pred.detach().cpu().numpy()
for i in range(y_true.shape[1]):
# AUC is only defined when there is at least one positive data.
if np.sum(y_true[:, i] == 1) > 0 and np.sum(y_true[:, i] == 0) > 0:
is_labeled = y_true[:, i] == y_true[:, i]
score = roc_auc_score(y_true[is_labeled, i], y_pred[is_labeled, i])
rocauc_list.append(score)
if len(rocauc_list) == 0:
raise RuntimeError(
'No positively labeled data available. Cannot compute ROC-AUC.')
return sum(rocauc_list) / len(rocauc_list)
@torch.no_grad()
def evaluate(model, dataset, split_idx, eval_func, result=None):
if result is not None:
out = result
else:
model.eval()
out = model(dataset)
train_acc = eval_func(
dataset.label[split_idx['train']], out[split_idx['train']])
valid_acc = eval_func(
dataset.label[split_idx['valid']], out[split_idx['valid']])
test_acc = eval_func(
dataset.label[split_idx['test']], out[split_idx['test']])
return train_acc, valid_acc, test_acc, out