Skip to content

Commit 3eb1e8e

Browse files
committed
make stack a double stack
1 parent 2b4a8be commit 3eb1e8e

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.1.0"
55

66
[deps]
77
MLJ = "add582a8-e3ab-11e8-2d5e-e98b27df1bc7"
8+
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
89
MLJTuning = "03970b2e-30c4-11ea-3135-d1576263f10f"
910
NearestNeighborModels = "636a865e-7cf4-491e-846c-de09b730eb36"
1011
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

examples/bigtest/Manifest.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,7 @@ version = "0.1.4"
851851

852852
[[deps.MLJTestIntegration]]
853853
deps = ["MLJ", "MLJTuning", "NearestNeighborModels", "Pkg", "Test"]
854-
git-tree-sha1 = "5c7a7ab6746c897e1904468cf0f9ef460ae1876d"
855-
repo-rev = "multi-threading"
856-
repo-url = "https://github.com/JuliaAI/MLJTestIntegration.jl"
854+
path = "/Users/anthony/MLJ/MLJTestIntegration"
857855
uuid = "697918b4-fdc1-4f9e-8ff9-929724cee270"
858856
version = "0.1.0"
859857

src/MLJTestIntegration.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module MLJTestIntegration
22

3-
const N_MODELS_FOR_REPEATABILITY_TEST = 50
3+
const N_MODELS_FOR_REPEATABILITY_TEST = 20
44

55
using MLJ
66
using Pkg

src/attemptors.jl

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ function evaluation(measure, model, resources, data...; throw=false, verbosity=1
131131
message = L > 1 ? "[:accelerated_evaluation] " : "[evaluation] "
132132
message *= "Evaluating model performance using $L different resources. "
133133
attempt(finalize(message, verbosity); throw) do
134-
es = map(resources) do accel
134+
es = map(resources) do resource
135135
evaluate(model, data...;
136136
measure=measure,
137137
resampling=Holdout(),
138-
acceleration=accel,
138+
acceleration=resource,
139139
verbosity=0)
140140
end
141141
ms = map(e->sort(e.per_fold[1]), es)
@@ -156,7 +156,7 @@ function tuned_pipe_evaluation(
156156
attempt(finalize(message, verbosity); throw) do
157157
pipe = identity |> model
158158
tuned_pipe = TunedModel(
159-
models=[pipe,],
159+
models=fill(pipe, 3),
160160
measure=measure,
161161
)
162162
evaluate(
@@ -194,6 +194,31 @@ function iteration_prediction(measure, model, data...; throw=false, verbosity=1)
194194
end
195195
end
196196

197+
function _stack(model, resource, isregressor)
198+
if isregressor
199+
models = (knn1=KNNRegressor(K=4),
200+
knn2=KNNRegressor(K=6),
201+
tmodel=model)
202+
metalearner = KNNRegressor()
203+
else
204+
models = (knn1=KNNClassifier(K=4),
205+
knn2=KNNClassifier(K=6),
206+
tmodel=model)
207+
metalearner = KNNClassifier()
208+
end
209+
Stack(;
210+
metalearner,
211+
resampling=CV(;nfolds=2),
212+
acceleration=resource,
213+
models...
214+
)
215+
end
216+
217+
# return a nested stack in which `model` appears at two levels, with
218+
# both layers accelerated using `resource`:
219+
_double_stack(model, resource, isregressor) =
220+
_stack(_stack(model, resource, isregressor), resource, isregressor)
221+
197222
# the `model` can only be single-target deterministic regressor or
198223
# probabilistic classifier.
199224
function stack_evaluation(
@@ -205,32 +230,17 @@ function stack_evaluation(
205230
)
206231
L = length(resources)
207232
message = L > 1 ? "[:accelerated_stack_evaluation] " : "[stack_evaluation] "
208-
message *= "Evaluating a stack containing model "*
209-
"with $L different resources. "
233+
message *= "Evaluating a nested stack containing model "*
234+
"using $L different resources. "
210235
target_scitype = MLJ.target_scitype(model)
211-
if AbstractVector{Continuous} <: target_scitype
212-
models = (knn1=KNNRegressor(K=4),
213-
knn2=KNNRegressor(K=6),
214-
model=model)
215-
metalearner = KNNRegressor()
216-
measure = LPLoss(2)
217-
else
218-
models = (knn1=KNNClassifier(K=4),
219-
knn2=KNNClassifier(K=6),
220-
model=model)
221-
metalearner = KNNClassifier()
222-
measure = BrierScore()
223-
end
224-
attempt(finalize(message, verbosity); throw) do
225-
es = map(resources) do accel
226-
mystack = Stack(
227-
; metalearner,
228-
resampling=CV(;nfolds=3),
229-
acceleration=accel,
230-
models...)
236+
isregressor = AbstractVector{Continuous} <: target_scitype
237+
measure = isregressor ? LPLoss(2) : BrierScore()
231238

239+
attempt(finalize(message, verbosity); throw) do
240+
es = map(resources) do resource
241+
stack = _stack(model, resource, isregressor)
232242
evaluate(
233-
mystack,
243+
stack,
234244
data...;
235245
measure=measure,
236246
resampling=Holdout(),
@@ -239,7 +249,6 @@ function stack_evaluation(
239249
end |> collect
240250
ms = map(e->sort(e.per_fold[1]), es)
241251
m = first(ms)
242-
# @show ms
243252
@assert all((m), ms[2:end]) ERR_INCONSISTENT_RESULTS
244253
first(es)
245254
end

src/test.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ These additional tests are applied to `Supervised` models:
147147
- `:iteration_prediction`: If the model is iterable, repeat the
148148
`:evaluation` test but first wrap as an `IteratedModel`.
149149
150-
- `:stack_evaluation`: make the model one of three base models in a
151-
`Stack`, and evaluate the `Stack`.
150+
- `:stack_evaluation`: test a `Stack` within a `Stack`, with the model
151+
being tested appearing at two levels, and evaluate the
152+
`Stack`. (Other base models and adjudicators in the double stack are
153+
instances of `KNNClassifier` or `KNNRegressor`.)
154+
This test is only applied to single target supervised models that
155+
are probabilistic classifiers or deterministic regressors.
152156
153157
- `:accelerated_stack_evaluation`: If the model appears to make
154158
repeatable predictions on retraining, check consistency of
155159
evaluations for `Stack(acceleration=CPU1(), ...)` and
156-
`Stack(acceleration=CPUThreads(), ...)`. This test is only applied
157-
to single target supervised models that are probabilistic
158-
classifiers or deterministic regressors.
160+
`Stack(acceleration=CPUThreads(), ...)` (in the double stack above).
159161
160162
"""
161163
function test(model_proxies, data...; mod=Main, level=2, throw=false, verbosity=1,)
@@ -338,7 +340,7 @@ function test(model_proxies, data...; mod=Main, level=2, throw=false, verbosity=
338340
verbosity > 1 && println(" Repeatable.")
339341
else
340342
verbosity > 1 && println(" Not repeatable.")
341-
end
343+
end
342344
end
343345

344346
length(resources) > 1 && verbosity > 0 &&

0 commit comments

Comments
 (0)