Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check compatibility of initial and param_info #97

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion R/sim_anneal_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,31 @@ random_real_neighbor <- function(current, hist_values, pset, retain = 1,

encode_set_backwards <- function(x, pset, ...) {
pset <- pset[pset$id %in% names(x), ]
new_vals <- purrr::map2(pset$object, x, dials::encode_unit, direction = "backward")
new_vals <- mapply(encode_unit_backwards, pset$object, x,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transitioned to mapply() so that we don't get purrr's Error in index i, caused by [call we can't control] rethrown error formatting.

SIMPLIFY = FALSE, USE.NAMES = FALSE)
names(new_vals) <- names(x)
tibble::as_tibble(new_vals)
}

encode_unit_backwards <- function(x, value) {
simonpcouch marked this conversation as resolved.
Show resolved Hide resolved
if (!dials::has_unknowns(x)) {
compl <- value[!is.na(value)]
if (any(compl < 0) | any(compl > 1)) {
simonpcouch marked this conversation as resolved.
Show resolved Hide resolved
cli::cli_abort(c(
"!" = "The parameter set used when tuning generating the initial \\
results isn't compatible with the parameter set supplied \\
as {.arg param_info}.",
"i" = "Possible values of parameters in {.arg param_info} should \\
encompass all values evaluated in the initial grid."
),
call = rlang::call2("tune_sim_anneal()")
)
}
}

dials::encode_unit(x, value, direction = "backward")
}

sample_by_distance <- function(candidates, existing, retain, pset) {
if (nrow(existing) > 0) {
existing <- tune::encode_set(existing, pset, as_matrix = TRUE)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/_snaps/sa-overall.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,18 @@
9 ( ) accept suboptimal roc_auc=0.8623 (+/-0.007265)
10 <3 new best roc_auc=0.86273 (+/-0.007569)

# incompatible parameter objects

Code
res <- tune_sim_anneal(car_wflow, param_info = parameter_set_with_smaller_range,
resamples = car_folds, initial = tune_res_with_bigger_range, iter = 2)
Message
Optimizing rmse
Initial best: 2284.10000
Condition
Error in `tune_sim_anneal()`:
! The parameter set used when tuning generating the initial results isn't compatible with the parameter set supplied as `param_info`.
i Possible values of parameters in `param_info` should encompass all values evaluated in the initial grid.
Message
x Optimization stopped prematurely; returning current results.

41 changes: 41 additions & 0 deletions tests/testthat/test-sa-overall.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,47 @@ test_that("unfinalized parameters", {
})
})

test_that("incompatible parameter objects", {
skip_on_cran()

skip_if_not_installed("ranger")
skip_if_not_installed("modeldata")
skip_if_not_installed("rsample")


rf_spec <- parsnip::rand_forest(mode = "regression", mtry = tune::tune())

grid_with_bigger_range <-
dials::grid_latin_hypercube(dials::mtry(range = c(1, 16)))

set.seed(1)
car_folds <- rsample::vfold_cv(car_prices, v = 2)

car_wflow <- workflows::workflow() %>%
workflows::add_formula(Price ~ .) %>%
workflows::add_model(rf_spec)

tune_res_with_bigger_range <- tune::tune_grid(
car_wflow,
resamples = car_folds,
grid = grid_with_bigger_range
)

parameter_set_with_smaller_range <-
dials::parameters(dials::mtry(range = c(1, 5)))

expect_snapshot(error = TRUE, {
res <-
tune_sim_anneal(
car_wflow,
param_info = parameter_set_with_smaller_range,
resamples = car_folds,
initial = tune_res_with_bigger_range,
iter = 2
)
})
})

test_that("set event-level", {
# See issue 40
skip_if_not_installed("rpart")
Expand Down
Loading