-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathseeloss.py
86 lines (75 loc) · 2.89 KB
/
seeloss.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
82
83
84
85
86
'''
Authors: Kieu My
'''
from matplotlib import pyplot as pl
def readfile(filename):
with open(filename) as fp:
data = fp.readlines()
data = [item.rstrip() for item in data]
tr_losses,fscores, precision,recall= [],[],[],[]
for line in data:
if line[0] != '#':
try:
epoch, _tr_loss, tr_loss, _score,score,_pre, pre, _rec, rec = line.split(' ')
tr_losses.append(float(tr_loss))
fscores.append(float(score))
precision.append(float(pre))
recall.append(float(rec))
except:
print('expected: epoch: int yloss: float clsloss: float clsacc: float lr: float correct: float precision: float recall: float')
print('received: ',line)
return tr_losses,fscores,precision,recall
#### This function plot multi curves, each curve is one name
def plot_multicurves(list_curves, list_labels,start=0):
list_colors = ['red', 'blue', 'green', 'black', 'pink', 'violet','yellow','cyan']
ncurve = len(list_curves)
min_len = min([len(list_curves[i]) for i in range(ncurve)])
print('Plot ',ncurve,'curves from epoch ',start,' to epoch ',min_len)
print('You can start from other epochs by add start value at the end of plot_multicurves(...,start) function')
for i in range(ncurve):
for j in range(start):
list_curves[i][j] = list_curves[i][start]
list_curves[i] = list_curves[i][:min_len]
epoch = range(1,min_len+1)
pl.figure(figsize=(8, 5))
for i in range(ncurve):
pl.plot(epoch, list_curves[i], color=list_colors[i], label=list_labels[i])
pl.xlabel('number of epochs')
pl.ylabel('percent')
pl.grid(True)
pl.legend()
# pl.show()
def plot_one_curve(curve, name,start=0):
print('Plot ', name, ' curve from epoch ', start, ' to the end ')
print('You can start from other epochs by add start value at the end of plot_one_curve(...,start) function')
print('epoch = ',len(curve))
for i in range(start):
curve[i] = curve[start]
# curve = curve[start:]
epoch = range(1,len(curve)+1)
print('epoch = ',epoch)
pl.figure(figsize=(8, 5))
pl.plot(epoch, curve, color='red', label=name)
pl.xlabel('epochs')
pl.ylabel('value')
pl.grid(True)
pl.legend()
# pl.show()
def scale0_1(curve):
vmin = min(curve)
vmax = max(curve)
ncurve = [(x-vmin)/(vmax-vmin) for x in curve]
return ncurve
def plot():
ylosses, fscore, precision, recall = readfile('backup/savelog.txt')
plot_one_curve(ylosses,'training loss')
ylosses = scale0_1(ylosses)
plot_multicurves([ylosses,fscore,precision, recall], ['training loss','validation fscore', 'validation precision','validation recall'])
pl.show()
if __name__ == '__main__':
import sys
if len(sys.argv) >=1:
plot()
else:
print('Usage:')
print(' python seeloss.py')