From dfcd6d784a04315dd5ab1f5c0111082fe789291e Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Fri, 19 Jan 2024 15:25:43 -0600 Subject: [PATCH] finalize parameters with unknown ranges in `tune_sim_anneal()` --- NEWS.md | 2 ++ R/tune_sim_anneal.R | 12 ++++---- tests/testthat/_snaps/sa-overall.md | 43 +++++++++++++++++++++-------- tests/testthat/test-sa-overall.R | 9 +++++- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/NEWS.md b/NEWS.md index 473e173..26b2da6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # finetune (development version) +* Fixed bug where `tune_sim_anneal()` would fail when supplied parameters needing finalization. The function will now finalize needed parameter ranges internally (#39). + * `autoplot()` methods for racing objects will now use integers in x-axis breaks (#75). * Enabling the `verbose_elim` control option for `tune_race_anova()` will now additionally introduce a message confirming that the function is evaluating against the burn-in resamples. diff --git a/R/tune_sim_anneal.R b/R/tune_sim_anneal.R index 9f6439a..abe2dc1 100644 --- a/R/tune_sim_anneal.R +++ b/R/tune_sim_anneal.R @@ -328,12 +328,12 @@ tune_sim_anneal_workflow <- } # In case mtry or other parameters with missing ranges need finalization - # param_info <- tune::check_parameters( - # workflow = object, - # pset = param_info, - # data = resamples$splits[[1]]$data, - # grid_names = param_info$id - # ) + param_info <- tune::check_parameters( + wflow = object, + pset = param_info, + data = resamples$splits[[1]]$data, + grid_names = character(0) + ) tune::check_workflow(object, check_dials = !is.null(param_info), pset = param_info) diff --git a/tests/testthat/_snaps/sa-overall.md b/tests/testthat/_snaps/sa-overall.md index f2209cd..0149e8d 100644 --- a/tests/testthat/_snaps/sa-overall.md +++ b/tests/testthat/_snaps/sa-overall.md @@ -205,16 +205,37 @@ set.seed(40) rf_res_finetune <- wf_rf %>% tune_sim_anneal(resamples = bt, initial = rf_res) Message + i Creating pre-processing data to finalize unknown parameter: mtry Optimizing roc_auc - Initial best: 0.86248 - 1 ( ) accept suboptimal roc_auc=0.86132 (+/-0.007045) - 2 ( ) accept suboptimal roc_auc=0.85987 (+/-0.007598) - 3 + better suboptimal roc_auc=0.86155 (+/-0.007228) - 4 + better suboptimal roc_auc=0.86193 (+/-0.007212) - 5 ( ) accept suboptimal roc_auc=0.86108 (+/-0.00727) - 6 ( ) accept suboptimal roc_auc=0.85992 (+/-0.007641) - 7 + better suboptimal roc_auc=0.86045 (+/-0.007405) - 8 x restart from best roc_auc=0.85863 (+/-0.007596) - 9 ( ) accept suboptimal roc_auc=0.8623 (+/-0.007265) - 10 <3 new best roc_auc=0.86273 (+/-0.007569) + Initial best: 0.84856 + 1 ( ) accept suboptimal roc_auc=0.84375 (+/-0.007727) + 2 <3 new best roc_auc=0.84943 (+/-0.007036) + 3 ( ) accept suboptimal roc_auc=0.84371 (+/-0.007903) + 4 + better suboptimal roc_auc=0.84825 (+/-0.008036) + 5 ( ) accept suboptimal roc_auc=0.84479 (+/-0.00814) + 6 + better suboptimal roc_auc=0.84816 (+/-0.007283) + 7 ( ) accept suboptimal roc_auc=0.84381 (+/-0.007999) + 8 <3 new best roc_auc=0.85014 (+/-0.007172) + 9 ( ) accept suboptimal roc_auc=0.84344 (+/-0.007818) + 10 + better suboptimal roc_auc=0.84802 (+/-0.007281) + +--- + + Code + set.seed(40) + rf_res_finetune <- wf_rf %>% tune_sim_anneal(resamples = bt) + Message + i Creating pre-processing data to finalize unknown parameter: mtry + Optimizing roc_auc + Initial best: 0.84829 + 1 ( ) accept suboptimal roc_auc=0.84326 (+/-0.007765) + 2 <3 new best roc_auc=0.84843 (+/-0.007903) + 3 ( ) accept suboptimal roc_auc=0.84438 (+/-0.007904) + 4 <3 new best roc_auc=0.84881 (+/-0.007951) + 5 ( ) accept suboptimal roc_auc=0.84404 (+/-0.007787) + 6 <3 new best roc_auc=0.84912 (+/-0.007272) + 7 ( ) accept suboptimal roc_auc=0.84333 (+/-0.007685) + 8 + better suboptimal roc_auc=0.84867 (+/-0.007152) + 9 - discard suboptimal roc_auc=0.84525 (+/-0.007793) + 10 ( ) accept suboptimal roc_auc=0.84383 (+/-0.00773) diff --git a/tests/testthat/test-sa-overall.R b/tests/testthat/test-sa-overall.R index d666677..83af21f 100644 --- a/tests/testthat/test-sa-overall.R +++ b/tests/testthat/test-sa-overall.R @@ -101,7 +101,7 @@ test_that("unfinalized parameters", { rec_example <- recipe(Class ~ ., data = two_class_dat) # RF - model_rf <- rand_forest(min_n = tune()) %>% + model_rf <- rand_forest(mtry = tune()) %>% set_mode("classification") %>% set_engine("ranger") @@ -118,6 +118,13 @@ test_that("unfinalized parameters", { rf_res_finetune <- wf_rf %>% tune_sim_anneal(resamples = bt, initial = rf_res) }) + + # don't supply an initial grid (#39) + expect_snapshot({ + set.seed(40) + rf_res_finetune <- wf_rf %>% + tune_sim_anneal(resamples = bt) + }) }) test_that("set event-level", {