-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
81 lines (73 loc) · 2.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
import matplotlib.pyplot as plt
def lossplot(x, y):
plt.ioff()
plt.plot(x, y, linestyle='-', color='b')
plt.title('Average loss per epoch')
plt.xlabel('epoch')
plt.ylabel('avg loss')
plt.show()
def lossplot_with_val(x, y1, y2):
"""
:param y1: training loss
:param y2: validation loss
"""
plt.ioff()
plt.plot(x, y1, linestyle='-', color='b', label = "training")
plt.plot(x, y2, linestyle='-', color='r', label = "validation")
plt.title('Average loss per epoch')
plt.xlabel('epoch')
plt.ylabel('avg loss')
plt.legend()
plt.show()
def lossplot_active(x, y):
plt.ion()
plt.plot(x, y, linestyle='-', color='b')
plt.title('Average loss per epoch')
plt.xlabel('epoch')
plt.ylabel('avg loss')
plt.show()
plt.pause(0.0001)
def lossplot_with_val_active(x, y1, y2):
"""
:param y1: training loss
:param y2: validation loss
"""
plt.ion()
plt.plot(x, y1, linestyle='-', color='b', label = "training")
plt.plot(x, y2, linestyle='-', color='r', label = "validation")
plt.title('Average loss per epoch')
plt.xlabel('epoch')
plt.ylabel('avg loss')
plt.legend()
plt.show()
plt.pause(0.0001)
def dscm(x, y):
tp, fp, tn, fn = 0, 0, 0, 0
for i in range(x.shape[0]):
if x[i] == 1:
if x[i] == y[i]:
tp += 1
else:
fp += 1
else:
if x[i] == y[i]:
tn += 1
else:
fn += 1
# acc = (tp+tn)/(tp + fp + tn + fn)
# sen = tp/(tp+fn)
# spe = tn/(tn+fp)
if (tp + fp + tn + fn) != 0:
print(f"acc = {(tp + tn) / (tp + fp + tn + fn)}")
else:
print("ERROR - acc : (tp + fp + tn + fn) = 0")
if (tp + fn) != 0:
print(f"sen = {tp / (tp + fn)}")
else:
print("ERROR - sen : (tp+fn) = 0")
if (tn + fp) != 0:
print(f"spe = {tn / (tn + fp)}")
else:
print("ERROR - spe : (tn+fp) = 0")
return tp, fp, tn, fn
# return acc, sen, spe