-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
65 lines (49 loc) · 1.88 KB
/
metrics.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
58
59
60
from sklearn.metrics import recall_score, precision_score, f1_score, roc_auc_score
import numpy as np
import torch
import torch.nn.functional as F
__all__ = ['flat_accuracy', 'recall_m', 'precision', 'f1_score_m', 'auc_score']
def flat_accuracy(preds, labels):
preds = preds[0]
preds = F.softmax(preds, dim=1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
pred_flat = np.argmax(preds, axis = 1).flatten()
labels_flat = labels.flatten()
acc = float(np.sum(pred_flat == labels_flat)) / len(labels_flat)
return torch.tensor(acc)
def recall_m(preds, labels):
preds = preds[0]
preds = F.softmax(preds, dim=1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
pred_flat = np.argmax(preds, axis = 1).flatten()
labels_flat = labels.flatten()
recall = recall_score(y_true = labels_flat, y_pred = pred_flat, average='binary')
return torch.tensor(recall)
def precision(preds, labels):
preds = preds[0]
preds = F.softmax(preds, dim=1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
pred_flat = np.argmax(preds, axis = 1).flatten()
labels_flat = labels.flatten()
recall = precision_score(y_true = labels_flat, y_pred = pred_flat, average='binary')
return torch.tensor(recall)
def f1_score_m(preds, labels):
preds = preds[0]
preds = F.softmax(preds, dim=1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
pred_flat = np.argmax(preds, axis = 1)
labels_flat = labels
f1_score_ = f1_score(y_true = labels_flat, y_pred = pred_flat, average='binary')
return torch.tensor(f1_score_)
def auc_score(preds, labels):
preds = F.softmax(preds[0], dim=1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
labels_flat = labels
proba_1 = preds[:, 1]
auc = roc_auc_score(y_true = labels_flat, y_score = proba_1)
return torch.tensor(auc)