|
| 1 | +#' @title Advanced version of autoRLearn. |
| 2 | +#' |
| 3 | +#' @description Tunes the hyperparameters of the desired algorithm/s using either hyperband or BOHB. |
| 4 | +#' |
| 5 | +#' @param df_train Dataframe of the training dataset. Assumes it is in perfect shape with all numeric variables and factor response variable named "class". |
| 6 | +#' @param df_test Dataframe of the test dataset. Assumes it is in perfect shape with all numeric variables and factor response variable named "class". |
| 7 | +#' @param maxTime Float representing the maximum time the algorithm should be run (seconds). |
| 8 | +#' @param models List of strings denoting which algorithms to use for the process: |
| 9 | +#' \itemize{ |
| 10 | +#' \item "randomForest" - Random forests using the randomForest package |
| 11 | +#' \item "ranger - Random forests using the ranger package (unstable) |
| 12 | +#' \item "naiveBayes" - Naive bayes using the fastNaiveBayes package |
| 13 | +#' \item "boosting" - Gradient boosting using xgboost |
| 14 | +#' \item "l2-linear-classifier" - Linear primal Support vector machine from LibLinear |
| 15 | +#' \item "svm" - RBF kernel svm from e1071 |
| 16 | +#' } |
| 17 | +#' @param optimizationAlgorithm - String of which hyperparameter tuning algorithm to use: |
| 18 | +#' \itemize{ |
| 19 | +#' \item "hyperband" - Hyperband with uniformly initiated parameters |
| 20 | +#' \item "bohb" - Hyperband with bayesian optimization as described on F. Hutter et al 2018 paper BOHB. Has extra parameters bw and kde_type |
| 21 | +#' } |
| 22 | +#' @param bw - (only applies to BOHB) Double representing how much should the KDE bandwidth be widened. Higher values allow the algorithm to explore more hyperparameter combinations |
| 23 | +#' @param max_iter - (affects both hyperband and BOHB) Integer representing the maximum number of iterations that one successive halving run can have |
| 24 | +#' @param kde_type - (only applies to BOHB) String representing whether a model's hyperparameters should be tuned individually of each other or have their probability densities multiplied: |
| 25 | +#' \itemize{ |
| 26 | +#' \item "single" - each hyperparameter has its own expected improvement calculated |
| 27 | +#' \item "mixed" - all hyperparameters' probabilty densities are multiplied and only one mixed expected improvement is calculated |
| 28 | +#' } |
| 29 | +#' @return List of Results |
| 30 | +#' \itemize{ |
| 31 | +#' \item \code{perf} - accuracy of the best performing model on the test data |
| 32 | +#' \item \code{pred} - prediction on the test data using the best model |
| 33 | +#' \item \code{model} - best model object |
| 34 | +#' \item \code{best_models} - table with the best hyperparameters found for the selected models. |
| 35 | +#' } |
| 36 | + |
| 37 | +#' @importFrom R.utils withTimeout |
| 38 | +#' @importFrom tictoc tic toc |
| 39 | + |
| 40 | +#' @export autoRLearn_ |
| 41 | +autoRLearn_ <- function(df_train, df_test, maxTime = 10, models = c("randomForest", "naiveBayes", "boosting", "l2-linear-classifier", "svm"), optimizationAlgorithm = "hyperband", bw = 3, max_iter = 81, kde_type = "single") { |
| 42 | + |
| 43 | + total_time = maxTime * 60 |
| 44 | + |
| 45 | + parameters_per_model <- map_int(models, .f = ~ length(jsons[[.x]]$params)) |
| 46 | + |
| 47 | + times = (parameters_per_model * total_time) / (sum(parameters_per_model)) |
| 48 | + |
| 49 | + print("Time distribution:") |
| 50 | + print(times) |
| 51 | + print("Models selected:") |
| 52 | + print(models) |
| 53 | + |
| 54 | + run_optimization = function(model, time) { |
| 55 | + |
| 56 | + results = NULL |
| 57 | + |
| 58 | + priors = data.frame() |
| 59 | + |
| 60 | + tic(model, "optimization time:") |
| 61 | + |
| 62 | + if(optimizationAlgorithm == "hyperband") { |
| 63 | + |
| 64 | + current <- Sys.time() %>% as.integer() |
| 65 | + |
| 66 | + end <- (Sys.time() %>% as.integer()) + time |
| 67 | + |
| 68 | + repeat { |
| 69 | + |
| 70 | + gc(verbose = F) |
| 71 | + |
| 72 | + tic("current hyperband runtime") |
| 73 | + |
| 74 | + print(paste("started", model)) |
| 75 | + |
| 76 | + time_left <- max(end - (Sys.time() %>% as.integer()), 1) |
| 77 | + |
| 78 | + print(paste("There are:", time_left, "seconds left for this hyperband run")) |
| 79 | + |
| 80 | + res <- hyperband(df = df_train, model = model, max_iter = max_iter, maxtime = time_left) |
| 81 | + |
| 82 | + if(is_empty(flatten(res)) == F) { |
| 83 | + |
| 84 | + res <- res %>% |
| 85 | + map_dfr(.f = ~ .x[["answer"]]) %>% |
| 86 | + arrange(desc(acc)) %>% |
| 87 | + head(1) |
| 88 | + |
| 89 | + results <- c(list(res), results) |
| 90 | + |
| 91 | + print(paste('Best accuracy from hyperband this round: ', res$acc)) |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + elapsed <- (Sys.time() %>% as.integer()) - current |
| 96 | + |
| 97 | + if(elapsed >= time) { |
| 98 | + |
| 99 | + break |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + else if(optimizationAlgorithm == "bohb") { |
| 108 | + |
| 109 | + current <- Sys.time() %>% as.integer() |
| 110 | + |
| 111 | + end <- (Sys.time() %>% as.integer()) + time |
| 112 | + |
| 113 | + repeat { |
| 114 | + |
| 115 | + gc(verbose = F) |
| 116 | + |
| 117 | + tic("current bohb time") |
| 118 | + |
| 119 | + print(paste("started", model)) |
| 120 | + |
| 121 | + time_left <- max(end - (Sys.time() %>% as.integer()), 1) |
| 122 | + |
| 123 | + print(paste("There are:", time_left, "seconds left for this bohb run")) |
| 124 | + |
| 125 | + res <- bohb(df = df_train, model = model, bw = bw, max_iter = max_iter, maxtime = time_left, priors = priors, kde_type = kde_type) |
| 126 | + |
| 127 | + if(is_empty(flatten(res)) == F) { |
| 128 | + |
| 129 | + priors <- res %>% |
| 130 | + map_dfr(.f = ~ .x[["sh_runs"]]) |
| 131 | + |
| 132 | + res <- res %>% |
| 133 | + map_dfr(.f = ~ .x[["answer"]]) %>% |
| 134 | + arrange(desc(acc)) %>% |
| 135 | + head(1) |
| 136 | + |
| 137 | + results <- c(list(res), results) |
| 138 | + |
| 139 | + print(paste('Best accuracy from hyperband this round: ', res$acc)) |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + elapsed <- (Sys.time() %>% as.integer()) - current |
| 144 | + |
| 145 | + if(elapsed >= time) { |
| 146 | + |
| 147 | + break |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + else { |
| 157 | + |
| 158 | + errorCondition(message = "Only hyperband and bohb are valid optimization algorithms at this moment.") |
| 159 | + |
| 160 | + break |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + toc() |
| 165 | + |
| 166 | + results |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + print("Finished all optimizations.") |
| 171 | + |
| 172 | + ans = vector(mode = "list", length = length(models)) |
| 173 | + |
| 174 | + |
| 175 | + for(i in 1:length(models)) { |
| 176 | + |
| 177 | + flag <- TRUE |
| 178 | + |
| 179 | + tryCatch(expr = { |
| 180 | + |
| 181 | + ans[[i]] <- run_optimization(models[[i]], times[[i]]) |
| 182 | + |
| 183 | + }, error = function(e) { |
| 184 | + |
| 185 | + print("Error spotted, going to the next model") |
| 186 | + |
| 187 | + flag <<- FALSE |
| 188 | + |
| 189 | + }) |
| 190 | + |
| 191 | + if (!flag) next |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + ans = ans %>% |
| 196 | + map(.f = ~ map_dfr(.x = .x, .f = ~ .x %>% select(model, params, acc))) %>% |
| 197 | + map_dfr(.f = ~ .x %>% arrange(desc(acc)) %>% head(1)) %>% |
| 198 | + arrange(desc(acc)) |
| 199 | + |
| 200 | + best_model <- ans %>% head(1) |
| 201 | + |
| 202 | + final_evaluation <- eval_loss(model = best_model[["model"]], train_df = df_train, test_df = df_test, params = best_model[["params"]]) |
| 203 | + |
| 204 | + final_evaluation$best_models <- ans |
| 205 | + |
| 206 | + print(paste("Winner:", best_model$model, "test accuracy:", final_evaluation$perf)) |
| 207 | + |
| 208 | + final_evaluation |
| 209 | + |
| 210 | +} |
0 commit comments