-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmetric.py
More file actions
36 lines (27 loc) · 997 Bytes
/
metric.py
File metadata and controls
36 lines (27 loc) · 997 Bytes
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
import numpy as np
import scipy.sparse as sp
import torch
def encode_onehot(labels):
classes = set(labels)
classes_dict = {c: np.identity(len(classes))[i, :] for i, c in
enumerate(classes)}
labels_onehot = np.array(list(map(classes_dict.get, labels)),
dtype=np.int32)
return labels_onehot
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
def roc_auc_compute_fn(y_preds, y_targets):
try:
from sklearn.metrics import roc_auc_score
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")
y_true = y_targets.cpu().numpy()
y_true = encode_onehot(y_true)
y_pred = y_preds.cpu().detach().numpy()
return roc_auc_score(y_true, y_pred)
def prec_recall_n(output, labels, topn):
preds = output.detach().numpy()[-1]
pass