-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
155 lines (120 loc) · 4.07 KB
/
plot.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import sys
from matplotlib import pyplot as pl
logFile = sys.argv[1]
# - Round 200 (global_step=199, learning_rate=0.001) -
def parseRoundHeader(l):
parts = l.split(' ')
trainRound = int(parts[2]) # '' 'Round' '200'
return trainRound
# Greedy (best) 0.8790 (matched 0.8550, longerDer 0.0020, shorterDer: 0.0030, betterScore: 0.0190)
def parseEvalLine(l):
# "best) 0.8790 "
parts = l.split("matched")
leftParts = parts[0][:-1].strip().split(" ")
totalText = leftParts[-1]
detailParts = parts[1][:-1].split(' ')
betterText = detailParts[-1][:-1]
return float(totalText), float(betterText)
def parseLog(logPath):
taskName=None
X = []
bestY = []
bestBetterY = []
stopY = []
stopBetterY = []
incBetterX = [] # only track improvements for incumbent
incBetterY = []
lastIncumbent=0.0
trainRound=None
for l in open(logPath, 'r'):
if l.startswith("- Round "):
trainRound = parseRoundHeader(l)
X.append(trainRound)
elif "(best)" in l:
if trainRound is None:
print("Missed round header!!!")
raise SystemExit
# total, match, longerDer, shortedDer, betterScore = parseEvalLine(l)
bestTotal, bestBetter = parseEvalLine(l)
bestY.append(bestTotal)
bestBetterY.append(bestBetter)
elif "(STOP)" in l:
if trainRound is None:
print("Missed round header!!!")
raise SystemExit
# total, match, longerDer, shortedDer, betterScore = parseEvalLine(l)
stopTotal, stopBetter = parseEvalLine(l)
stopY.append(stopTotal)
stopBetterY.append(stopBetter)
elif "Incumbent" in l:
if trainRound is None:
print("Missed round header!!!")
raise SystemExit
# total, match, longerDer, shortedDer, betterScore = parseEvalLine(l)
incTotal, incBetter = parseEvalLine(l)
if incBetter <= lastIncumbent:
continue
lastIncumbent=incBetter
incBetterX.append(trainRound)
incBetterY.append(incBetter)
elif l.startswith("name ="):
taskName = l.split(" ")[2].strip()
return taskName, X, bestY, bestBetterY, stopY, stopBetterY, incBetterX, incBetterY
# parse data
taskName, X, bestY, bestBetterY, stopY, stopBetterY, incBetterX, incBetterY = parseLog(sys.argv[1])
# build an artificial performance score
def buildPerfIndex(bestY, bestBetterY):
return [best + better for best, better in zip(bestY, bestBetterY)]
def filterImprovements(X,Y):
outX = [X[0]]
outY = [Y[0]]
bestY= outY[0]
for x,y in zip(X,Y):
if y > bestY:
outX.append(x)
outY.append(y)
bestY = y
return outX, outY
indexY = buildPerfIndex(bestY, bestBetterY)
### plot data ###
bestX=X[:len(bestY)]
# filter new incumbents
topX, topY = filterImprovements(bestX, bestY)
bestBetterX = X[:len(bestBetterY)]
topBetterX, topBetterY = filterImprovements(bestBetterX, bestBetterY)
stopBetterX = X[:len(stopBetterY)]
topStopX, topStopY = filterImprovements(stopBetterX, stopBetterY)
# plot training performance index
pl.plot(X[:len(indexY)], indexY, 'k:',label="perf. index")
# plot data
def plotAnnotated(rawX, Y, color, label):
X = rawX[:len(Y)]
topX, topY = filterImprovements(X, Y)
pl.plot(topX, topY, '{}o'.format(color)) # best seen solution
pl.plot(X, Y, color, label=label)
# current hit rate
plotAnnotated(X, bestY, "b", "opt: bestP")
plotAnnotated(X, stopY, "g", "opt: P@stop")
# fraction of improved programs
pl.plot(incBetterX[:len(incBetterY)], incBetterY, 'k.:',label="> ref (ceil)")
plotAnnotated(X, bestBetterY, "c", label="> ref (best P)")
plotAnnotated(X, stopBetterY, "m", label="> ref (P@stop)")
# extend incBetter line
if len(incBetterY) > 0:
# append line w/o dot
lastValue = incBetterY[-1]
lastUpdateRound = incBetterX[-1]
lastRound = X[-1]
if lastUpdateRound != lastRound:
pl.plot([lastUpdateRound, lastRound], [lastValue] * 2, 'k:')
# geknaupte axis
pl.plot([0, X[-1]], [1.0, 1.0], "k-")
pl.title("training on {} task".format(taskName))
### grid ###
# pl.rc('grid', linestyle=":", color='gray')
# pl.grid(True)
# axis
pl.xlabel("global step")
pl.ylabel("% of hold-out samples")
pl.legend()
pl.show()