-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcriteria.jl
468 lines (345 loc) · 12 KB
/
criteria.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
const PRECHELT_REF = "[Prechelt, Lutz (1998): \"Early Stopping"*
"- But When?\", in *Neural Networks: Tricks of the Trade*, "*
"ed. G. Orr, Springer.](https://link.springer.com/chapter"*
"/10.1007%2F3-540-49430-8_3)"
const STOPPING_DOC = "An early stopping criterion for loss-reporting "*
"iterative algorithms. "
const CUSTOM_ALTERNATIVE_DOC = "For a customizable loss-based stopping "*
"criterion, use [`WithLossDo`](@ref) or [`WithTrainingLossesDo`](@ref) "*
"with the `stop_if_true=true` option. "
## NEVER
"""
Never()
$STOPPING_DOC
Indicates early stopping is to be disabled.
See also [`NotANumber`](@ref), for stopping on encountering `NaN`.
"""
struct Never <: StoppingCriterion end
## OUT OF BOUNDS
"""
InvalidValue()
$STOPPING_DOC
Stop if a loss (or training loss) is `NaN`, `Inf` or `-Inf` (or, more
precisely, if `isnan(loss)` or `isinf(loss)` is `true`).
$CUSTOM_ALTERNATIVE_DOC
"""
struct InvalidValue <: StoppingCriterion end
_isinf(x) = isinf(x)
_isinf(::Nothing) = false
_isnan(x) = isnan(x)
_isnan(::Nothing) = false
# state = `true` when `NaN`, `Inf` or `-Inf` has been encountered
update(::InvalidValue, loss, state=false) =
state !== nothing && state || _isinf(loss) || _isnan(loss)
update_training(c::InvalidValue, loss, state) = update(c, loss, state)
done(::InvalidValue, state) = state !== nothing && state
message(::InvalidValue, state) = "Stopping early as `NaN`, "*
"`Inf` or `-Inf` encountered. "
## TIME LIMIT
"""
TimeLimit(; t=0.5)
$STOPPING_DOC
Stopping is triggered after `t` hours have elapsed since the stopping
criterion was initiated.
Any Julia built-in `Real` type can be used for `t`. Subtypes of
`Period` may also be used, as in `TimeLimit(t=Minute(30))`.
Internally, `t` is rounded to nearest millisecond.
``
"""
struct TimeLimit <: StoppingCriterion
t::Millisecond
function TimeLimit(t::Millisecond)
t > Millisecond(0) ||
throw(ArgumentError("Time limit `t` must be positive. "))
return new(t)
end
end
TimeLimit(t::T) where T <: Period = TimeLimit(convert(Millisecond, t))
# for t::T a "numeric" time in hours; assumes `round(Int, ::T)` implemented:
TimeLimit(t) = TimeLimit(round(Int, 3_600_000*t) |> Millisecond)
TimeLimit(; t =Minute(30)) = TimeLimit(t)
# state = time at initialization
update(::TimeLimit, loss, ::Nothing) = now()
update_training(::TimeLimit, loss, ::Nothing) = now()
done(criterion::TimeLimit, state) =
state === nothing ? false : criterion.t < now() - state
## GL
# helper:
generalization_loss(E, E_opt) = 100*(E/abs(E_opt) - one(E_opt))
"""
GL(; alpha=2.0)
$STOPPING_DOC
A stop is triggered when the (rescaled) generalization loss exceeds
the threshold `alpha`.
**Terminology.** Suppose ``E_1, E_2, ..., E_t`` are a sequence of
losses, for example, out-of-sample estimates of the loss associated
with some iterative machine learning algorithm. Then the
*generalization loss* at time `t`, is given by
`` GL_t = 100 (E_t - E_{opt}) \\over |E_{opt}|``
where ``E_{opt}`` is the minimum value of the sequence.
Reference: $PRECHELT_REF.
"""
struct GL <: StoppingCriterion
alpha::Float64
function GL(alpha)
alpha > 0 ||
throw(ArgumentError("Threshold `alpha` must be positive. "))
return new(alpha)
end
end
GL(; alpha=2.0) = GL(alpha)
update(::GL, loss, ::Nothing) = (loss=loss, min_loss=loss)
update(::GL, loss, state) = (loss=loss, min_loss=min(loss, state.min_loss))
function done(criterion::GL, state)
if state === nothing
return false
else
gl = generalization_loss(state.loss, state.min_loss)
return gl > criterion.alpha
end
end
## PQ
# helpers:
function prepend(v, x, max_length)
if length(v) < max_length
vcat([x, ], v)
else
vcat([x, ], v[1:end-1])
end
end
progress(losses::AbstractVector{T}) where T =
1000*(mean(losses)/minimum(losses) - one(T))
_min(x, ::Nothing) = x
_min(::Nothing, x) = x
_min(x, y) = min(x, y)
"""
PQ(; alpha=0.75, k=5, tol=eps(Float64))
A stopping criterion for training iterative supervised learners.
A stop is triggered when Prechelt's progress-modified generalization
loss exceeds the threshold ``PQ_T > alpha``, or if the training progress drops
below ``P_j ≤ tol``. Here `k` is the number of training (in-sample) losses used to
estimate the training progress.
## Context and explanation of terminology
The *training progress* at time ``j`` is defined by
`` P_j = 1000 |M - m|/|m| ``
where ``M`` is the mean of the last `k` training losses ``F_1, F_2, …, F_k``
and ``m`` is the minimum value of those losses.
The *progress-modified generalization loss* at time ``t`` is then given by
`` PQ_t = GL_t / P_t``
where ``GL_t`` is the generalization loss at time ``t``; see
[`GL`](@ref).
PQ will stop when the following are true:
1) At least `k` training samples have been collected via
`done!(c::PQ, loss; training = true)` or `update_training(c::PQ, loss, state)`
2) The last update was an out-of-sample update.
(`done!(::PQ, loss; training=true)` is always false)
3) The progress-modified generalization loss exceeds the threshold
``PQ_t > alpha`` **OR** the training progress stalls ``P_j ≤ tol``.
Reference: $PRECHELT_REF.
"""
struct PQ <: StoppingCriterion
alpha::Float64
k::Union{Int,Float64} # could be Inf
tol::Float64
function PQ(alpha, k, tol)
alpha > 0 ||
throw(ArgumentError("Threshold `alpha` must be positive. "))
if k < 2 || !(k isa Int || isinf(k))
throw(ArgumentError("`k` must `Int` or `Inf` and `k > 1`. "))
end
tol > 0 ||
throw(ArgumentError("`tol` must be positive. "))
return new(alpha, k, tol)
end
end
PQ(; alpha=0.75, k=5, tol=eps(Float64)) = PQ(alpha, k, tol)
struct PQState{T}
training_losses::Vector{T}
waiting_for_out_of_sample::Bool
loss::Union{Nothing,T}
min_loss::Union{Nothing,T}
end
update_training(::PQ, loss, ::Nothing) = PQState([loss, ], true, nothing, nothing)
update(::PQ, loss::T, ::Nothing) where T = PQState(T[], false, loss, loss)
function update_training(criterion::PQ, loss, state)
training_losses = prepend(state.training_losses, loss, criterion.k)
return PQState(training_losses, true, state.loss, state.min_loss)
end
function update(::PQ, loss, state)
min_loss = _min(loss, state.min_loss)
return PQState(state.training_losses, false, loss, min_loss)
end
function done(criterion::PQ, state)
state === nothing && return false
state.loss === nothing && return false
state.waiting_for_out_of_sample && return false
length(state.training_losses) < criterion.k && return false
GL = generalization_loss(state.loss, state.min_loss)
P = progress(state.training_losses)
P > criterion.tol || return true
PQ = GL/P
return PQ > criterion.alpha
end
## PATIENCE
# This is UP_s in Prechelt 1998
"""
Patience(; n=5)
$STOPPING_DOC
A stop is triggered by `n` consecutive increases in the loss.
Denoted "_UP_s" in $PRECHELT_REF.
$CUSTOM_ALTERNATIVE_DOC
"""
struct Patience <: StoppingCriterion
n::Int
function Patience(n::Int)
n > 0 ||
throw(ArgumentError("The patience level `n` must be positive. "))
return new(n)
end
end
Patience(; n=5) = Patience(n)
# Prechelt alias:
const UP = Patience
update(::Patience, loss, ::Nothing) = (loss=loss, n_increases=0)
@inline function update(::Patience, loss, state)
old_loss, n = state
if loss > old_loss
n += 1
else
n = 0
end
return (loss=loss, n_increases=n)
end
done(criterion::Patience, state) =
state === nothing ? false : state.n_increases == criterion.n
## NUMBER SINCE BEST
"""
NumberSinceBest(; n=6)
$STOPPING_DOC
A stop is triggered when the number of calls to the control, since the
lowest value of the loss so far, is `n`.
$CUSTOM_ALTERNATIVE_DOC
"""
struct NumberSinceBest <: StoppingCriterion
n::Int
function NumberSinceBest(n::Int)
n > 0 ||
throw(ArgumentError("`n` must be positive. "))
return new(n)
end
end
NumberSinceBest(; n=6) = NumberSinceBest(n)
update(::NumberSinceBest, loss, ::Nothing) = (best=loss, number_since_best=0)
@inline function update(::NumberSinceBest, loss, state)
best, number_since_best = state
if loss < best
best = loss
number_since_best = 0
else
number_since_best += 1
end
return (best=best, number_since_best=number_since_best)
end
done(criterion::NumberSinceBest, state) =
state === nothing ? false : state.number_since_best == criterion.n
# # NUMBER LIMIT
"""
NumberLimit(; n=100)
$STOPPING_DOC
A stop is triggered by `n` consecutive loss updates, excluding
"training" loss updates.
If wrapped in a `stopper::EarlyStopper`, this is the number of calls
to `done!(stopper)`.
"""
struct NumberLimit <: StoppingCriterion
n::Int
function NumberLimit(n::Int)
n > 0 ||
throw(ArgumentError("`n` must be positive. "))
return new(n)
end
end
NumberLimit(; n=100) = NumberLimit(n)
update(criterion::NumberLimit, loss, ::Nothing) = 1
update(::NumberLimit, loss, state) = state+1
done(criterion::NumberLimit, state) =
state === nothing ? false : state >= criterion.n
# ## THRESHOLD
"""
Threshold(; value=0.0)
$STOPPING_DOC
A stop is triggered as soon as the loss drops below `value`.
$CUSTOM_ALTERNATIVE_DOC
"""
struct Threshold <: StoppingCriterion
value::Float64
end
Threshold(; value=0.0) = Threshold(value)
update(::Threshold, loss, state) = loss
done(criterion::Threshold, state) =
state === nothing ? false : state < criterion.value
"""
Warmup(c::StoppingCriterion, n)
Wait for `n` updates before checking stopping criterion `c`
"""
struct Warmup{C} <: StoppingCriterion where {C <: StoppingCriterion}
criterion::C
n::Int
function Warmup(criterion::C, n::N) where {C <: StoppingCriterion, N <: Integer}
n > 0 || throw(ArgumentError("`n` must be positive. "))
new{C}(criterion, Int(n))
end
end
# Constructors for Warmup
Warmup() = Warmup(InvalidValue()) # Default for testing
Warmup(c; n = 1) = Warmup(c, n) # Provide kwargs interface
# Initialize inner state for type-stability, and record first observation
update(c::Warmup, loss, ::Nothing) = update(c, loss)
update(criterion::Warmup, loss) = (1, update(criterion.criterion, loss))
# Catch uninitialized state
update_training(c::Warmup, loss, ::Nothing) = update_training(c, loss)
update_training(c::Warmup, loss) = (1, update_training(c.criterion, loss))
# Handle update vs update_training
update(c::Warmup, loss, state) = _update(update, c, loss, state)
update_training(c::Warmup, loss, state) =
_update(update_training, c, loss, state)
# Dispatch update and update_training here
function _update(f::Function, criterion::Warmup, loss, state)
n, inner = state
n += 1
if n <= criterion.n
# Skip inner criterion
return n, inner
elseif n == criterion.n+1
# First step of inner criterion
return n, f(criterion.criterion, loss)
else
# Step inner criterion
return n, f(criterion.criterion, loss, inner)
end
end
function done(criterion::Warmup, state)
# Only check if inner criterion is done after n updates
state === nothing && return false
return state[1] <= criterion.n ? false : done(criterion.criterion, state[2])
end
message(c::Warmup, state) = message(c.criterion, state[2])
## NOT A NUMBER (deprecated)
"""
NotANumber()
$STOPPING_DOC
Stop if a loss of `NaN` is encountered.
**Now deprecated** in favour of [`InvalidValue`](@ref).
"""
struct NotANumber <: StoppingCriterion
function NotANumber()
Base.depwarn("`NotANumber()` is deprecated. Use `InvalidValue()` "*
"to trap `NaN`, `Inf` or `-Inf`. ", :NotANumber)
return new()
end
end
# state = `true` when NaN has been encountered
update(::NotANumber, loss, state) = state !== nothing && state || isnan(loss)
update_training(c::NotANumber, loss, state) = update(c, loss, state)
done(::NotANumber, state) = state !== nothing && state
message(::NotANumber, state) = "Stopping early as NaN encountered. "