-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathROC_DNN.py
66 lines (54 loc) · 2.06 KB
/
ROC_DNN.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
# 绘制ROC曲线
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset
import pandas as pd
from DNN import *
import numpy as np
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
def plot_roc_curve(y_true, y_pred, label):
# 将真实标签和预测标签转换为numpy数组
y_true = np.array(y_true.detach())
y_pred = np.array(y_pred.detach())
# 计算ROC曲线上的点
fpr, tpr, thresholds = roc_curve(y_true, y_pred, pos_label=1)
# 计算AUC值
auc_value = auc(fpr, tpr)
# 绘制ROC曲线
plt.plot(fpr, tpr, label='{} ROC area = %0.6f'.format(label) % auc_value)
plt.plot([0, 1], [0, 1], 'k--') # 绘制对角线
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve'.format(label))
plt.legend(loc="lower right")
if __name__ == '__main__':
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)
dnn_relu = torch.load('dnn_RELU.pth')
dnn_relu = dnn_relu.to(device)
dnn_sigmoid = torch.load('dnn_SIGMOID.pth')
dnn_sigmoid = dnn_sigmoid.to(device)
dnn_tanh = torch.load('dnn_TANH.pth')
dnn_tanh = dnn_tanh.to(device)
test_set = dataset(train=False)
test_loader = DataLoader(test_set, batch_size=400, shuffle=False) # 一次全部计算完
for x_test, y_test in test_loader:
x_test = x_test.to(device)
y_test = y_test.to(device)
y_pre = dnn_relu(x_test)
plot_roc_curve(y_test, y_pre, 'relu')
for x_test, y_test in test_loader:
x_test = x_test.to(device)
y_test = y_test.to(device)
y_pre = dnn_tanh(x_test)
plot_roc_curve(y_test, y_pre, 'tanh')
for x_test, y_test in test_loader:
x_test = x_test.to(device)
y_test = y_test.to(device)
y_pre = dnn_sigmoid(x_test)
plot_roc_curve(y_test, y_pre, 'sigmoid')
plt.savefig('ROC curve', dpi=600)
plt.show()