-
Notifications
You must be signed in to change notification settings - Fork 35
hybrid grids #451
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
Open
topepo
wants to merge
2
commits into
main
Choose a base branch
from
hybrid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hybrid grids #451
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ Imports: | |
| rlang (>= 1.1.0), | ||
| sfd, | ||
| tibble, | ||
| tidyr, | ||
| utils, | ||
| vctrs (>= 0.3.8), | ||
| withr | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| #' Hybrid space-filling/regular parameter grids | ||
| #' | ||
| #' A grid can be created that is efficient for some parameter(s) but | ||
| #' fine-grained for others. This can be especially helpful when a model has | ||
| #' a "submodel" parameter that can be tuned with virtually no cost. | ||
| #' | ||
| #' | ||
| #' @inheritParams grid_regular | ||
| #' @inheritParams grid_space_filling | ||
| #' @param size An integer for the maximum size of the space-filling portion | ||
| #' of the design. | ||
| #' @param levels The number of values for _each_ regular grid parameter. | ||
| #' @param parameters A character string that matches the _names_ of the | ||
| #' parameter(s) that are used to make the regular portion of the grid. If no | ||
| #' value is given, a space-filling design with `size` candidates is created. If | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These fallbacks are silent on purpose. We might be able to move to use these grids as the default in tune (to support submodel grids), it didn't want it to be noisy. |
||
| #' all parameters are selected, a regular grid with `levels^p` candidates is | ||
| #' created where `p` is the length of `parameters`. | ||
| #' @details | ||
| #' | ||
| #' This function first creates a space-filling design for the parameters that | ||
| #' do not match `parameters` (with `size` total candidates). Then a regular grid | ||
| #' is created with `levels^p` candidates where `p` is the length of | ||
| #' `parameters`. These two grids are crossed to produce the end result. | ||
|
|
||
| #' @examples | ||
| #' if (rlang::is_installed("ggplot2")) { | ||
| #' | ||
| #' library(dplyr) | ||
| #' library(ggplot2) | ||
| #' | ||
| #' # Most boosting methods can make many predictions across a number of trees | ||
| #' # from a single model fit object. Those are nearly free (computationally) so | ||
| #' # we would do many tree values for each of the other parameters. | ||
| #' | ||
| #' # To illustrate, we'll only show four tree values: | ||
| #' | ||
| #' boost_example <- | ||
| #' parameters(list(trees = trees(), learn_rate = learn_rate(), min_n = min_n())) | ||
| #' | ||
| #' boost_example |> | ||
| #' grid_hybrid(parameters = "trees", size = 20, levels = 4) |> | ||
| #' ggplot(aes(learn_rate, min_n)) + | ||
| #' geom_point() + | ||
| #' facet_wrap(~ trees, labeller = "label_both") + | ||
| #' scale_x_log10() | ||
| #' | ||
| #' # In other cases, we have 1+ parameters with very few values. We can make a | ||
| #' # small regular grid over these and a much larger space-filling design | ||
| #' | ||
| #' nnet_example <- | ||
| #' parameters( | ||
| #' list( | ||
| #' learn_rate = learn_rate(), | ||
| #' dropout = dropout(), | ||
| #' # Only a few values: | ||
| #' activation = activation(c("relu", "tanh", "elu")), | ||
| #' schedule = rate_schedule(c("none", "cyclic", "step")) # note different name | ||
| #' ) | ||
| #' ) | ||
| #' | ||
| #' nnet_example |> | ||
| #' grid_hybrid(parameters = c("activation", "schedule"), size = 20, levels = 3) |> | ||
| #' ggplot(aes(learn_rate, dropout)) + | ||
| #' geom_point() + | ||
| #' facet_grid(activation ~ schedule, labeller = "label_both") + | ||
| #' scale_x_log10() | ||
| #' } | ||
| #' | ||
| #' @export | ||
| grid_hybrid <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| UseMethod("grid_hybrid") | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname grid_hybrid | ||
| grid_hybrid.default <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| if (missing(x)) { | ||
| cli::cli_abort("At least one parameter object is required.") | ||
| } | ||
| cli::cli_abort( | ||
| "{.arg x} must be a {.cls param} object, list, or {.cls parameters} object, | ||
| not {.obj_type_friendly {x}}." | ||
| ) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname grid_hybrid | ||
| grid_hybrid.parameters <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| check_dots_empty() | ||
|
|
||
| if (nrow(x) == 0) { | ||
| cli::cli_abort("At least one parameter object is required.") | ||
| } | ||
| for (i in seq_along(x$object)) { | ||
| check_param( | ||
| x$object[[i]], | ||
| allow_na = FALSE, | ||
| allow_unknown = FALSE, | ||
| arg = x$id[i] | ||
| ) | ||
| } | ||
|
|
||
| grd <- make_hybrid( | ||
| x, | ||
| parameters = parameters, | ||
| size = size, | ||
| levels = levels, | ||
| original = original, | ||
| type = type | ||
| ) | ||
| grd | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname grid_hybrid | ||
| grid_hybrid.list <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| check_dots_empty() | ||
|
|
||
| if (length(x) == 0) { | ||
| cli::cli_abort("At least one parameter object is required.") | ||
| } | ||
| param_names <- names(x) | ||
| for (i in seq_along(x)) { | ||
| check_param( | ||
| x[[i]], | ||
| allow_na = FALSE, | ||
| allow_unknown = FALSE, | ||
| arg = param_arg_name(param_names[i], x[[i]], i) | ||
| ) | ||
| } | ||
|
|
||
| params <- parameters(x) | ||
| grd <- make_hybrid( | ||
| params, | ||
| parameters = parameters, | ||
| size = size, | ||
| levels = levels, | ||
| original = original, | ||
| type = type | ||
| ) | ||
| grd | ||
| } | ||
|
|
||
|
|
||
| #' @export | ||
| #' @rdname grid_hybrid | ||
| grid_hybrid.param <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| param_list <- list(x, ...) | ||
| param_names <- names(param_list) | ||
| for (i in seq_along(param_list)) { | ||
| check_param( | ||
| param_list[[i]], | ||
| allow_na = FALSE, | ||
| allow_unknown = FALSE, | ||
| arg = param_arg_name(param_names[i], param_list[[i]], i) | ||
| ) | ||
| } | ||
|
|
||
| params <- parameters(param_list) | ||
| grd <- make_hybrid( | ||
| params, | ||
| parameters = parameters, | ||
| size = size, | ||
| levels = levels, | ||
| original = original, | ||
| type = type | ||
| ) | ||
| grd | ||
| } | ||
|
|
||
|
|
||
| make_hybrid <- function( | ||
| x, | ||
| ..., | ||
| parameters = NULL, | ||
| size = 10, | ||
| levels = 20, | ||
| original = TRUE, | ||
| type = "any" | ||
| ) { | ||
| if (is.null(parameters)) { | ||
| res <- grid_space_filling(x, size = size, original = original, type = type) | ||
| return(res) | ||
| } else { | ||
| reg_param <- x$id %in% parameters | ||
| if (!any(reg_param)) { | ||
| cli::cli_abort( | ||
| "The {.arg parameters} argument value {.val {parameters}} did not select | ||
| any of the parameter identifiers: {.val {x$id}}" | ||
| ) | ||
| } else if (all(reg_param)) { | ||
| res <- grid_regular(x, levels = levels, original = original) | ||
| return(res) | ||
| } | ||
| } | ||
|
|
||
| sfd_param <- x[!reg_param, ] | ||
| reg_param <- x[reg_param, ] | ||
|
|
||
| sfd <- grid_space_filling( | ||
| sfd_param, | ||
| size = size, | ||
| original = original, | ||
| type = type | ||
| ) | ||
| reg <- grid_regular(reg_param, levels = levels, original = original) | ||
| tidyr::crossing(sfd, reg) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use tidyselect here? I had to add a tidyr dep for
crossingand didn't know if tidyselect would be too much.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tidyselect is used in tidyr, so it doesn't add more weight IMO