forked from tidymodels/tune
-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.R
110 lines (100 loc) · 4.96 KB
/
control.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#' Control the grid search process
#'
#' @param verbose A logical for logging results as they are generated. Despite
#' this argument, warnings and errors are always shown.
#' @param allow_par A logical to allow parallel processing (if a parallel
#' backend is registered).
#' @param extract An optional function with at least one argument (or `NULL`)
#' that can be used to retain arbitrary objects from the model fit object,
#' recipe, or other elements of the workflow.
#' @param save_pred A logical for whether the out-of-sample predictions should
#' be saved for each model _evaluated_.
#' @param pkgs An optional character string of R package names that should be
#' loaded (by namespace) during parallel processing.
#'
#' @details
#'
#' For `extract`, this function can be used to output the model object, the
#' recipe (if used), or some components of either or both. When evaluated, the
#' function's sole argument has a named list with elements `recipe` and `model`.
#' If the formula method is used, the recipe element will be `NULL`.
#'
#' The results of the `extract` function are added to a list column in the
#' output called `.extracts`. Each element of this list is a tibble with tuning
#' parameter column and a list column (also called `.extracts`) that contains
#' the results of the function. If no extraction function is used, there is no
#' `.extracts` column in the resulting object.
#' @export
grid_control <- function(verbose = FALSE, allow_par = TRUE,
extract = NULL, save_pred = FALSE,
pkgs = NULL) {
# add options for seeds per resample
val_class_and_single(verbose, "logical", "grid_control()")
val_class_and_single(allow_par, "logical", "grid_control()")
val_class_and_single(save_pred, "logical", "grid_control()")
val_class_or_null(pkgs, "character", "grid_control()")
val_class_or_null(extract, "function", "grid_control()")
list(verbose = verbose, allow_par = allow_par, extract = extract,
save_pred = save_pred, pkgs = pkgs)
}
# ------------------------------------------------------------------------------
#' Control the Bayesian search process
#'
#' @param verbose A logical for logging results as they are generated. Despite
#' this argument, warnings and errors are always shown.
#' @param no_improve The integer cutoff for the number of iterations without
#' better results.
#' @param uncertain The number of iterations with no improvement before an
#' uncertainty sample is created where a sample with high predicted variance
#' is chosen. The iteration counter is reset after each uncertainty sample.
#' For example, if `uncertain = 10`, this condition is triggered every 10
#' samples with no improvement.
#' @param seed An integer for controlling the random number stream.
#' @param extract An optional function to collect any information from the model
#' or other objects. See `grid_control()` for details. Note that if initial
#' results were already generated using `tune_grid()`, care must be taken if
#' the Bayesian search has a different extraction function.
#' @param save_pred A logical to save the out-of-sample predictions from each
#' resample and each parameter combination. See `grid_control()` for details.
#' @param time_limit A number for the minimum number of _minutes_ (elapsed) that
#' the function should execute. The elapsed time is evaluated at internal
#' checkpoints and, if over time, the results at that time are returned (with
#' a warning). This means that the `time_limit` is not an exact limit, but a
#' minimum time limit.
#' @param pkgs An optional character string of R package names that should be
#' loaded (by namespace) during parallel processing.
#' @export
Bayes_control <-
function(verbose = FALSE,
no_improve = 10L,
uncertain = Inf,
seed = sample.int(10^5, 1),
extract = NULL,
save_pred = FALSE,
time_limit = NA,
pkgs = NULL) {
# add options for seeds per resample
val_class_and_single(verbose, "logical", "Bayes_control()")
val_class_and_single(save_pred, "logical", "Bayes_control()")
val_class_and_single(no_improve, c("numeric", "integer"), "Bayes_control()")
val_class_and_single(uncertain, c("numeric", "integer"), "Bayes_control()")
val_class_and_single(seed, c("numeric", "integer"), "Bayes_control()")
val_class_or_null(extract, "function", "Bayes_control()")
val_class_and_single(time_limit, c("logical", "numeric"), "Bayes_control()")
val_class_or_null(pkgs, "character", "Bayes_control()")
if (!is.infinite(uncertain) && uncertain > no_improve) {
cli::cli_alert_warning(
"Uncertainty sample scheduled after {uncertain} poor iterations but the search will stop after {no_improve}."
)
}
list(
verbose = verbose,
no_improve = no_improve,
uncertain = uncertain,
seed = seed,
extract = extract,
save_pred = save_pred,
time_limit = time_limit,
pkgs = pkgs
)
}