-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (53 loc) · 2.06 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns # import this after torch or it will break everything
from io import BytesIO
import torch
from sklearn.metrics import roc_curve,auc,precision_recall_curve,precision_score,recall_score
def squared_difference(a, b, do_normalization=True):
"""Computes (a-b) ** 2."""
if do_normalization:
a = torch.nn.functional.normalize(a, p=2, dim=1)
b = torch.nn.functional.normalize(b, p=2, dim=1)
return -2. * a.mm(b.t())
return torch.norm(a, dim=1, keepdim=True)**2 + \
(torch.norm(b, dim=1, keepdim=True)**2).t() - \
2. * a.mm(b.t())
def plot_roc(labels, scores, filename="", modelname="", save_plots=False):
fpr, tpr, _ = roc_curve(labels, scores)
roc_auc = auc(fpr, tpr)
# plot roc
if save_plots:
plt.figure()
plt.plot(fpr, tpr, color='darkorange',
lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title(f'Receiver operating characteristic {modelname}')
plt.legend(loc="lower right")
# plt.show()
plt.savefig(filename)
plt.close()
return roc_auc
def plot_pr(labels, scores, filename="", modelname="", save_plots=False):
precision, recall, _ = precision_recall_curve(labels, scores)
pr_auc = auc(recall, precision)
# plot pr
if save_plots:
plt.figure()
plt.plot(recall, precision, color='darkorange',
lw=2, label='PR curve (area = %0.2f)' % pr_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title(f'Precision-Recall {modelname}')
plt.legend(loc="lower right")
# plt.show()
plt.savefig(filename)
plt.close()
return pr_auc