From 48dd01722916b9f2c9b1f966cf4fea0e36ff1238 Mon Sep 17 00:00:00 2001 From: rikhuijzer Date: Wed, 1 Jun 2022 13:22:32 +0200 Subject: [PATCH 1/5] Allow uninstantiated `model` --- Project.toml | 2 +- src/tuned_models.jl | 14 ++++++++++++-- test/runtests.jl | 6 +----- test/tuned_models.jl | 7 +++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index c51915d..917f72f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MLJTuning" uuid = "03970b2e-30c4-11ea-3135-d1576263f10f" authors = ["Anthony D. Blaom "] -version = "0.7.1" +version = "0.7.2" [deps] ComputationalResources = "ed09eef8-17a6-5b46-8889-db040fac31e3" diff --git a/src/tuned_models.jl b/src/tuned_models.jl index 853c74d..7b011fc 100644 --- a/src/tuned_models.jl +++ b/src/tuned_models.jl @@ -260,11 +260,11 @@ function TunedModel(args...; model=nothing, # user can specify model as argument instead of kwarg: length(args) < 2 || throw(ERR_TOO_MANY_ARGUMENTS) - if length(args) === 1 + if length(args) == 1 arg = first(args) model === nothing || @warn warn_double_spec(arg, model) - model =arg + model = arg end # either `models` is specified and `tuning` is set to `Explicit`, @@ -309,7 +309,17 @@ function TunedModel(args...; model=nothing, else throw(ERR_MODEL_TYPE) end + elseif model isa Type && model <: Model + # Model is an uninstantiated model. + M = model + try + model = model() + catch + msg = "Unable to instantiate model $model; pass an instantiated model." + error(MethodError(msg)) + end else + # Model is probably an instantiated model. M = typeof(model) end diff --git a/test/runtests.jl b/test/runtests.jl index 9877ccc..75d0d61 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,11 +9,7 @@ using StableRNGs # Display Number of processes and if necessary number # of Threads @info "nworkers: $(nworkers())" -@static if VERSION >= v"1.3.0-DEV.573" - @info "nthreads: $(Threads.nthreads())" -else - @info "Running julia $(VERSION). Multithreading tests excluded. " -end +@info "nthreads: $(Threads.nthreads())" include("test_utilities.jl") diff --git a/test/tuned_models.jl b/test/tuned_models.jl index c512d9a..334db89 100644 --- a/test/tuned_models.jl +++ b/test/tuned_models.jl @@ -17,10 +17,10 @@ N = 30 x1 = rand(N); x2 = rand(N); x3 = rand(N); -X = (x1=x1, x2=x2, x3=x3); +X = (; x1, x2, x3); y = 2*x1 .+ 5*x2 .- 3*x3 .+ 0.4*rand(N); -m(K) = KNNRegressor(K=K) +m(K) = KNNRegressor(; K) r = [m(K) for K in 13:-1:2] # TODO: replace the above with the line below and post an issue on @@ -66,6 +66,9 @@ r = [m(K) for K in 13:-1:2] tm = @test_logs TunedModel(model=first(r), range=r, measure=rms) @test tm.tuning isa RandomSearch @test input_scitype(tm) == Table(Continuous) + + # Allow uninstantiated model. + @test TunedModel(; model=KNNRegressor, range=r).model isa KNNRegressor end results = [(evaluate(model, X, y, From f699f67d5aa066c79c153b9916907edbd8942093 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Tue, 7 Jun 2022 21:49:36 +0200 Subject: [PATCH 2/5] Throw clear error --- src/tuned_models.jl | 13 +++++-------- test/tuned_models.jl | 3 +-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/tuned_models.jl b/src/tuned_models.jl index 7b011fc..1883075 100644 --- a/src/tuned_models.jl +++ b/src/tuned_models.jl @@ -310,14 +310,11 @@ function TunedModel(args...; model=nothing, throw(ERR_MODEL_TYPE) end elseif model isa Type && model <: Model - # Model is an uninstantiated model. - M = model - try - model = model() - catch - msg = "Unable to instantiate model $model; pass an instantiated model." - error(MethodError(msg)) - end + msg = """ + Uninstantiated $model passed; pass an instantiated model so that this package \ + can mutate the hyperparameters specified by the range. + """ + throw(AssertionError(msg)) else # Model is probably an instantiated model. M = typeof(model) diff --git a/test/tuned_models.jl b/test/tuned_models.jl index 334db89..6bf5ea1 100644 --- a/test/tuned_models.jl +++ b/test/tuned_models.jl @@ -67,8 +67,7 @@ r = [m(K) for K in 13:-1:2] @test tm.tuning isa RandomSearch @test input_scitype(tm) == Table(Continuous) - # Allow uninstantiated model. - @test TunedModel(; model=KNNRegressor, range=r).model isa KNNRegressor + @test_throws AssertionError TunedModel(; model=KNNRegressor, range=r) end results = [(evaluate(model, X, y, From d3a9e05508b0fe6da6ffc44c17b25e4b937842b1 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Tue, 7 Jun 2022 21:56:35 +0200 Subject: [PATCH 3/5] Fix Julia 1.6 --- src/tuned_models.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/tuned_models.jl b/src/tuned_models.jl index 1883075..2e738ef 100644 --- a/src/tuned_models.jl +++ b/src/tuned_models.jl @@ -310,10 +310,8 @@ function TunedModel(args...; model=nothing, throw(ERR_MODEL_TYPE) end elseif model isa Type && model <: Model - msg = """ - Uninstantiated $model passed; pass an instantiated model so that this package \ - can mutate the hyperparameters specified by the range. - """ + msg = "Uninstantiated $model passed; pass an instantiated model so that this" * + "package can mutate the hyperparameters specified by the range." throw(AssertionError(msg)) else # Model is probably an instantiated model. From 3357691dc81b3705bffff7cdf73dc7389f7f29db Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Wed, 8 Jun 2022 10:57:20 +0200 Subject: [PATCH 4/5] Update src/tuned_models.jl Co-authored-by: Anthony Blaom, PhD --- src/tuned_models.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tuned_models.jl b/src/tuned_models.jl index 2e738ef..90156fe 100644 --- a/src/tuned_models.jl +++ b/src/tuned_models.jl @@ -309,9 +309,9 @@ function TunedModel(args...; model=nothing, else throw(ERR_MODEL_TYPE) end - elseif model isa Type && model <: Model - msg = "Uninstantiated $model passed; pass an instantiated model so that this" * - "package can mutate the hyperparameters specified by the range." + elseif model isa Type + msg = "Type encountered where model instance expected. (Tuning evaluates models "* + "generated by mutating clones of provided instance, as specified by `range`.) " throw(AssertionError(msg)) else # Model is probably an instantiated model. From 69f0faec809bfeb8a31ab3c0f406da52c18d93be Mon Sep 17 00:00:00 2001 From: rikhuijzer Date: Wed, 8 Jun 2022 11:11:27 +0200 Subject: [PATCH 5/5] Use const for error --- src/tuned_models.jl | 10 ++++++---- test/tuned_models.jl | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tuned_models.jl b/src/tuned_models.jl index 90156fe..29713c2 100644 --- a/src/tuned_models.jl +++ b/src/tuned_models.jl @@ -19,6 +19,10 @@ const ERR_BOTH_DISALLOWED = ArgumentError( "You cannot specify both `model` and `models`. ") const ERR_MODEL_TYPE = ArgumentError( "Only `Deterministic` and `Probabilistic` model types supported.") +const ERR_UNINSTANTIATED_MODEL = AssertionError( + "Type encountered where model instance expected. (Tuning evaluates "* + "models by mutating clones of the provided instance, as specified "* + "by `range`.) ") const INFO_MODEL_IGNORED = "`model` being ignored. Using `model=first(range)`. " const ERR_TOO_MANY_ARGUMENTS = @@ -309,10 +313,8 @@ function TunedModel(args...; model=nothing, else throw(ERR_MODEL_TYPE) end - elseif model isa Type - msg = "Type encountered where model instance expected. (Tuning evaluates models "* - "generated by mutating clones of provided instance, as specified by `range`.) " - throw(AssertionError(msg)) + elseif model isa Type + throw(ERR_UNINSTANTIATED_MODEL) else # Model is probably an instantiated model. M = typeof(model) diff --git a/test/tuned_models.jl b/test/tuned_models.jl index 6bf5ea1..bfec7a4 100644 --- a/test/tuned_models.jl +++ b/test/tuned_models.jl @@ -67,7 +67,7 @@ r = [m(K) for K in 13:-1:2] @test tm.tuning isa RandomSearch @test input_scitype(tm) == Table(Continuous) - @test_throws AssertionError TunedModel(; model=KNNRegressor, range=r) + @test_throws MLJTuning.ERR_UNINSTANTIATED_MODEL TunedModel(; model=KNNRegressor, range=r) end results = [(evaluate(model, X, y,