-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexplicit.jl
71 lines (56 loc) · 1.99 KB
/
explicit.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
const WARN_INCONSISTENT_PREDICTION_TYPE =
"Not all models to be evaluated have the same prediction type, and this may "*
"cause problems for some measures. For example, a probabilistic metric "*
"like `log_loss` cannot be applied to a model making point (deterministic) "*
"predictions. Inspect the prediction type with "*
"`prediction_type(model)`. "
mutable struct Explicit <: TuningStrategy end
struct ExplicitState{R, N}
range::R # a model-generating iterator
next::N # to hold output of `iterate(range)`
prediction_type::Symbol
user_warned::Bool
end
function MLJTuning.setup(tuning::Explicit, model, range, n, verbosity)
next = iterate(range)
return ExplicitState(range, next, MLJBase.prediction_type(model), false)
end
# models! returns as many models as possible but no more than `n_remaining`:
function MLJTuning.models(tuning::Explicit,
model,
history,
state,
n_remaining,
verbosity)
range, next, prediction_type, user_warned =
state.range, state.next, state.prediction_type, state.user_warned
function check(m)
if !user_warned && verbosity > -1 && MLJBase.prediction_type(m) != prediction_type
@warn WARN_INCONSISTENT_PREDICTION_TYPE
user_warned = true
end
end
next === nothing && return nothing, state
m, s = next
check(m)
models = Any[m, ] # types not known until run-time
next = iterate(range, s)
i = 1 # current length of `models`
while i < n_remaining
next === nothing && break
m, s = next
check(m)
push!(models, m)
i += 1
next = iterate(range, s)
end
new_state = ExplicitState(range, next, prediction_type, user_warned)
return models, new_state
end
function default_n(tuning::Explicit, range)
try
length(range)
catch MethodError
DEFAULT_N
end
end