/
plotlearningcurve.jl
283 lines (276 loc) · 9.11 KB
/
plotlearningcurve.jl
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
##### Beginning of file
import LaTeXStrings
import PGFPlots
import PGFPlotsX
import StatsBase
import ValueHistories
"""
"""
function plotlearningcurves(
inputobject::Fittable,
curvetype::Symbol = :loss_vs_iteration;
window::Integer = 0,
legendPos::AbstractString = "north east",
sampleevery::Integer = 1,
startat::Union{Integer, Symbol} = :start,
endat::Union{Integer, Symbol} = :end,
include_validation::Bool = true,
show_raw::Bool = true,
show_smoothed::Bool = true,
)
history = get_history(inputobject)
result = plotlearningcurve(
history,
curvetype;
window = window,
legendPos = legendPos,
sampleevery = sampleevery,
startat = startat,
endat = endat,
include_validation = include_validation,
show_raw = show_raw,
show_smoothed = show_smoothed,
)
return result
end
"""
"""
function plotlearningcurves(
history::ValueHistories.MultivalueHistory,
curvetype::Symbol = :loss_vs_iteration;
window::Integer = 0,
legendPos::AbstractString = "north east",
sampleevery::Integer = 1,
startat::Union{Integer, Symbol} = :start,
endat::Union{Integer, Symbol} = :end,
include_validation::Bool = true,
show_raw::Bool = true,
show_smoothed::Bool = true,
)
if curvetype == :loss_vs_iteration
has_validation = include_validation &&
haskey(history, :validation_loss_at_iteration)
xlabel = "Iteration"
ylabel = "Loss"
legendentry = "Loss function"
training_xvalues, training_yvalues = ValueHistories.get(
history,
:training_loss_at_iteration,
)
if has_validation
validation_xvalues, validation_yvalues = ValueHistories.get(
history,
:validation_loss_at_iteration,
)
end
elseif curvetype == :loss_vs_epoch
has_validation = include_validation &&
haskey(history, :validation_loss_at_epoch)
xlabel = "Epoch"
ylabel = "Loss"
legendentry = "Loss function"
training_xvalues, training_yvalues = ValueHistories.get(
history,
:training_loss_at_epoch,
)
if has_validation
validation_xvalues, validation_yvalues = ValueHistories.get(
history,
:validation_loss_at_epoch,
)
end
else
error("curvetype must be one of: :loss_vs_iteration, :loss_vs_epoch")
end
if length(training_xvalues) != length(training_yvalues)
error("length(training_xvalues) != length(training_yvalues)")
end
if has_validation
if length(training_xvalues) != length(validation_yvalues)
error("length(training_xvalues) != length(validation_yvalues)")
end
if length(training_xvalues) != length(validation_xvalues)
error("length(training_xvalues) != length(validation_xvalues)")
end
if !all(training_xvalues .== validation_xvalues)
error("!all(training_xvalues .== validation_xvalues)")
end
end
if startat == :start
startat = 1
elseif typeof(startat) <: Symbol
error("$(startat) is not a valid value for startat")
end
if endat == :end
endat = length(training_xvalues)
elseif typeof(endat) <: Symbol
error("$(endat) is not a valid value for endat")
end
if startat > endat
error("startat > endat")
end
training_xvalues = training_xvalues[startat:endat]
training_yvalues = training_yvalues[startat:endat]
if has_validation
validation_xvalues = validation_xvalues[startat:endat]
validation_yvalues = validation_yvalues[startat:endat]
end
if length(training_xvalues) != length(training_yvalues)
error("length(training_xvalues) != length(training_yvalues)")
end
if has_validation
if length(validation_xvalues) != length(validation_yvalues)
error("length(validation_xvalues) != length(validation_yvalues)")
end
if length(training_xvalues) != length(validation_xvalues)
error("length(training_xvalues) != length(validation_xvalues)")
end
if !all(training_xvalues .== validation_xvalues)
error("!all(training_xvalues .== validation_xvalues)")
end
result = plotlearningcurve(
training_xvalues,
training_yvalues,
xlabel,
ylabel,
legendentry;
window = window,
legendPos = legendPos,
sampleevery = sampleevery,
validation_yvalues = validation_yvalues,
show_raw = show_raw,
show_smoothed = show_smoothed,
)
else
result = plotlearningcurve(
training_xvalues,
training_yvalues,
xlabel,
ylabel,
legendentry;
window = window,
legendPos = legendPos,
sampleevery = sampleevery,
show_raw = show_raw,
show_smoothed = show_smoothed,
)
end
return result
end
"""
"""
function plotlearningcurves(
xvalues::AbstractVector{<:Real},
training_yvalues::AbstractVector{<:Real},
xlabel::AbstractString,
ylabel::AbstractString,
legendentry::AbstractString;
window::Integer = 0,
legendPos::AbstractString = "north east",
sampleevery::Integer = 1,
validation_yvalues::Union{Void, AbstractVector{<:Real}} = nothing,
show_raw::Bool = true,
show_smoothed::Bool = true,
)
if !show_raw && !show_smoothed
error("At least one of show_raw, show_smoothed must be true.")
end
if is_nothing(validation_yvalues)
has_validation = false
else
has_validation = true
end
if has_validation
training_legendentry = string(strip(legendentry),
", training set")
validation_legendentry = string(strip(legendentry),
", validation set")
else
training_legendentry = string(strip(legendentry),
", training set")
end
if sampleevery < 1
error("sampleevery must be >=1")
end
if length(xvalues) != length(training_yvalues)
error("length(xvalues) != length(training_yvalues)")
end
if length(xvalues) == 0
error("length(xvalues) == 0")
end
xvalues = xvalues[1:sampleevery:end]
training_yvalues = training_yvalues[1:sampleevery:end]
if has_validation
validation_yvalues = validation_yvalues[1:sampleevery:end]
end
allplotobjects = []
if show_raw
training_linearplotobject_yraw = PGFPlots.Plots.Linear(
xvalues,
training_yvalues,
legendentry = LaTeXStrings.LaTeXString(training_legendentry),
mark = "none",
)
push!(allplotobjects, training_linearplotobject_yraw)
if has_validation
validation_linearplotobject_yraw = PGFPlots.Plots.Linear(
xvalues,
validation_yvalues,
legendentry =
LaTeXStrings.LaTeXString(validation_legendentry),
mark = "none",
)
push!(allplotobjects, validation_linearplotobject_yraw)
end
#
end
if show_smoothed && window > 0
training_yvaluessmoothed = simple_moving_average(
training_yvalues,
window,
)
training_legendentry_smoothed = string(
strip(training_legendentry),
" (smoothed)",
)
training_linearplotobject_ysmoothed = PGFPlots.Plots.Linear(
xvalues,
training_yvaluessmoothed,
legendentry =
LaTeXStrings.LaTeXString(training_legendentry_smoothed),
mark = "none",
)
push!(allplotobjects, training_linearplotobject_ysmoothed)
#
if has_validation
validation_yvaluessmoothed = simple_moving_average(
validation_yvalues,
window,
)
validation_legendentry_smoothed = string(
strip(validation_legendentry),
" (smoothed)",
)
validation_linearplotobject_ysmoothed = PGFPlots.Plots.Linear(
xvalues,
validation_yvaluessmoothed,
legendentry =
LaTeXStrings.LaTeXString(
validation_legendentry_smoothed
),
mark = "none",
)
push!(allplotobjects, validation_linearplotobject_ysmoothed)
end
end
axisobject = PGFPlots.Axis(
[allplotobjects...],
xlabel = LaTeXStrings.LaTeXString(xlabel),
ylabel = LaTeXStrings.LaTeXString(ylabel),
legendPos = legendPos,
)
tikzpicture = PGFPlots.plot(axisobject)
return tikzpicture
end
const plotlearningcurve = plotlearningcurves
##### End of file