From 9b6005523cf2eca19f79c3af2952199f1c9da021 Mon Sep 17 00:00:00 2001 From: Marco Zanotti Date: Thu, 18 Jan 2024 09:06:00 +0100 Subject: [PATCH] up optimize section funzionante - error in feed-forward con decay=tune() --- dashboard/R/fit_model.R | 53 ++++++++++---- dashboard/todo.txt | 5 +- dashboard/tsf_dashboard.Rmd | 141 +++++++++++++++++++++++++++++++++++- 3 files changed, 176 insertions(+), 23 deletions(-) diff --git a/dashboard/R/fit_model.R b/dashboard/R/fit_model.R index 89ecafc..c84362f 100644 --- a/dashboard/R/fit_model.R +++ b/dashboard/R/fit_model.R @@ -355,10 +355,10 @@ generate_model_spec <- function(method, params) { } # function to generate the model specification for tuning -update_tune_model_parameters <- function(method, params) { +set_tune_parameters <- function(method, params) { - # function to get default tuning parameters - get_tune <- function(parameter, value) { + # function to set tuning parameters + set_tune <- function(parameter, value) { if (value == FALSE) { get_default(parameter) } else { @@ -367,19 +367,45 @@ update_tune_model_parameters <- function(method, params) { } mtd_params <- getOption("tsf.dashboard.methods_params")[[method]] # get the parameters for the method - # prm_name <- grep("_xx_", names(params), value = TRUE) # get the UI input id - # prm_ui_name <- eval(parse(text = paste0("params$", prm_name))) # get the UI parameters name if (method == "Elastic Net") { - prm_ui_name <- params$tune_xx_elanet + prm_ui_name <- params$tune_elanet + } else if (method == "MARS") { + prm_ui_name <- params$tune_mars + } else if (method == "KNN") { + prm_ui_name <- params$tune_knn + } else if (method == "SVM") { + if (params$tune_boundary == "Linear") { + prm_ui_name <- params$tune_svm_linear + } else { + prm_ui_name <- params$tune_svm_rbf + } } else if (method == "Random Forest") { - prm_ui_name <- params$tune_xx_rf + prm_ui_name <- params$tune_rf + } else if (method == "Boosted Trees") { + prm_ui_name <- params$tune_boost + } else if (method == "Cubist") { + prm_ui_name <- params$tune_cub + } else if (method == "Feed-Forward") { + prm_ui_name <- params$tune_ff + } else if (method == "Feed-Forward AR") { + prm_ui_name <- params$tune_ffar + } else if (method == "ARIMA-Boost") { + prm_ui_name <- params$tune_arima_boost + } else if (method == "Prophet-Boost") { + prm_ui_name <- params$tune_prophet_boost } else { stop(paste("Unknown method", method)) } tune_params <- mtd_params[names(mtd_params) %in% prm_ui_name] # get the parameters to tune is_to_tune <- mtd_params %in% tune_params - new_params <- purrr::map2(mtd_params, is_to_tune, get_tune) |> purrr::set_names(mtd_params) - # update_params <- c(params, new_params) + new_params <- purrr::map2(mtd_params, is_to_tune, set_tune) |> purrr::set_names(mtd_params) + + if (method == "SVM") { + new_params$boundary <- params$tune_boundary + } + if (method == "Boosted Trees") { + new_params$boost_method <- params$tune_boost_method + } return(new_params) @@ -503,7 +529,7 @@ fit_model_tuning <- function( seed = 1992 ) { - params_new <- update_tune_model_parameters(method, params) + params_new <- set_tune_parameters(method, params) check_parameters(method, params_new) set.seed(seed) @@ -529,11 +555,8 @@ fit_model_tuning <- function( # grid_spec <- generate_grid_spec(method, model_spec, grid_size, seed) # tuning - if (n_folds > 10 | grid_size > 25) { - doFuture::registerDoFuture() - future::plan(strategy = "multisession", workers = parallelly::availableCores() - 1) - message("Number of parallel workers: ", future::nbrOfWorkers()) - } + doFuture::registerDoFuture() + future::plan(strategy = "multisession", workers = parallelly::availableCores() - 1) tune_fit <- wkfl_spec |> tune::tune_grid( resamples = cv_splits, diff --git a/dashboard/todo.txt b/dashboard/todo.txt index 869903e..1c93e37 100644 --- a/dashboard/todo.txt +++ b/dashboard/todo.txt @@ -13,11 +13,7 @@ Next steps: - documentazione in alto a destra To Do: -- fare check senza if else in update_tune_model_parameters - tune_bayes -- finire update_tune_model_parameters con mapping parametri (occhio, non mtry ma rf_mtry) -- testare flusso optimize fino al forecast -- cambiare assegnazione nomi ai parametri in UI - aggiungere metodi di automl (h2o) - pensare e aggiungere la sezione di stacking (LM + Elastic Net) @@ -26,3 +22,4 @@ To Do: - aggiungere metodi di dl (NeuralProphet + NBEATS + DeepAR) - pensare e aggiungere la sezione di feature engineering (con in mente il save) - modificare output modello con parsing +- cambiare assegnazione nomi ai parametri in UI diff --git a/dashboard/tsf_dashboard.Rmd b/dashboard/tsf_dashboard.Rmd index d678862..07d7bf6 100644 --- a/dashboard/tsf_dashboard.Rmd +++ b/dashboard/tsf_dashboard.Rmd @@ -777,7 +777,7 @@ conditionalPanel( # Coming Soon! conditionalPanel( condition = "input.method == 'COMING SOON!'", - h5("New Deep Learning algorithms will be released soon!") + h5("New algorithms will be released soon!") ) ``` @@ -1077,7 +1077,7 @@ dropdownButton( pickerInput( inputId = "tune_method", label = h3("Forecast Algorithm"), multiple = FALSE, choices = list( - `Machine Learning` = ml_methods, + `Machine Learning` = ml_methods[-1], `Deep Learning` = dl_methods, `Mixed Algorithms` = mix_methods ), @@ -1110,24 +1110,157 @@ conditionalPanel( condition = "input.tune_method == 'Elastic Net'", h5("Algorithm hyperparameters to optimize: "), pickerInput( - inputId = "tune_xx_elanet", label = NULL, # _xx_ needed to recognize it easily in update_tune_model_parameters() + inputId = "tune_elanet", label = NULL, choices = mtd_prm_names[["Elastic Net"]], multiple = TRUE, selected = mtd_prm_names[["Elastic Net"]], options = list("actions-box" = TRUE) ) ) +# MARS +conditionalPanel( + condition = "input.tune_method == 'MARS'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_mars", label = NULL, + choices = mtd_prm_names[["MARS"]], multiple = TRUE, + selected = mtd_prm_names[["MARS"]][1:2], + options = list("actions-box" = TRUE) + ) +) + +# KNN +conditionalPanel( + condition = "input.tune_method == 'KNN'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_knn", label = NULL, + choices = mtd_prm_names[["KNN"]], multiple = TRUE, + selected = mtd_prm_names[["KNN"]], + options = list("actions-box" = TRUE) + ) +) + +# SVM +conditionalPanel( + condition = "input.tune_method == 'SVM'", + h5("Algorithm hyperparameters to optimize: "), + prettyRadioButtons( + inputId = "tune_boundary", label = "Boundary Type", choices = c("Linear", "Radial"), + inline = TRUE, selected = get_default("boundary") + ), + conditionalPanel( + condition = "input.tune_boundary == 'Linear'", + pickerInput( + inputId = "tune_svm_linear", label = NULL, multiple = TRUE, + choices = mtd_prm_names[["SVM"]][-c(1, 4)], selected = mtd_prm_names[["SVM"]][2:3], + options = list("actions-box" = TRUE) + ) + ), + conditionalPanel( + condition = "input.tune_boundary == 'Radial'", + pickerInput( + inputId = "tune_svm_rbf", label = NULL, multiple = TRUE, + choices = mtd_prm_names[["SVM"]][-1], selected = mtd_prm_names[["SVM"]][2:3], + options = list("actions-box" = TRUE) + ) + ) +) + # Random Forest conditionalPanel( condition = "input.tune_method == 'Random Forest'", h5("Algorithm hyperparameters to optimize: "), pickerInput( - inputId = "tune_xx_rf", label = NULL, # _xx_ needed to recognize it easily in update_tune_model_parameters() + inputId = "tune_rf", label = NULL, choices = mtd_prm_names[["Random Forest"]], multiple = TRUE, selected = mtd_prm_names[["Random Forest"]], options = list("actions-box" = TRUE) ) ) + +# Boosted Trees +conditionalPanel( + condition = "input.tune_method == 'Boosted Trees'", + h5("Algorithm hyperparameters to optimize: "), + prettyRadioButtons( + inputId = "tune_boost_method", label = "Boosting Method", + choices = c("XGBoost", "LightGBM"), + inline = TRUE, selected = get_default("boost_method") + ), + pickerInput( + inputId = "tune_boost", label = NULL, + choices = mtd_prm_names[["Boosted Trees"]][-1], multiple = TRUE, + selected = mtd_prm_names[["Boosted Trees"]][6], + options = list("actions-box" = TRUE) + ) +) + +# Cubist +conditionalPanel( + condition = "input.tune_method == 'Cubist'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_cub", label = NULL, + choices = mtd_prm_names[["Cubist"]], multiple = TRUE, + selected = mtd_prm_names[["Cubist"]], + options = list("actions-box" = TRUE) + ) +) + +# Feed-Forward +conditionalPanel( + condition = "input.tune_method == 'Feed-Forward'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_ff", label = NULL, + choices = mtd_prm_names[["Feed-Forward"]], multiple = TRUE, + selected = mtd_prm_names[["Feed-Forward"]][1:3], + options = list("actions-box" = TRUE) + ) +) + +# Feed-Forward AR +conditionalPanel( + condition = "input.tune_method == 'Feed-Forward AR'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_ffar", label = NULL, + choices = mtd_prm_names[["Feed-Forward AR"]], multiple = TRUE, + selected = mtd_prm_names[["Feed-Forward AR"]][2:4], + options = list("actions-box" = TRUE) + ) +) + +# ARIMA-Boost +conditionalPanel( + condition = "input.tune_method == 'ARIMA-Boost'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_arima_boost", label = NULL, + choices = mtd_prm_names[["ARIMA-Boost"]], multiple = TRUE, + selected = mtd_prm_names[["ARIMA-Boost"]][5], + options = list("actions-box" = TRUE) + ) +) + +# Prophet-Boost +conditionalPanel( + condition = "input.tune_method == 'Prophet-Boost'", + h5("Algorithm hyperparameters to optimize: "), + pickerInput( + inputId = "tune_prophet_boost", label = NULL, + choices = mtd_prm_names[["Prophet-Boost"]], multiple = TRUE, + selected = mtd_prm_names[["Prophet-Boost"]][5], + options = list("actions-box" = TRUE) + ) +) + +# Coming Soon! +conditionalPanel( + condition = "input.tune_method == 'COMING SOON!'", + h5("New algorithms will be released soon!") +) ``` ```{r}