diff --git a/R-package/R/aliases.R b/R-package/R/aliases.R new file mode 100644 index 000000000000..80d411f074b1 --- /dev/null +++ b/R-package/R/aliases.R @@ -0,0 +1,15 @@ +# Central location for paramter aliases. +# See https://lightgbm.readthedocs.io/en/latest/Parameters.html#core-parameters + +# [description] List of respected parameter aliases. Wrapped in a function to take advantage of +# lazy evaluation (so it doesn't matter what order R sources files during installation). +# [return] A named list, where each key is a main LightGBM parameter and each value is a character +# vector of corresponding aliases. +.PARAMETER_ALIASES <- function(){ + return(list( + "boosting" = c( + "boosting_type" + , "boost" + ) + )) +} diff --git a/R-package/R/callback.R b/R-package/R/callback.R index 6895d11f422f..577ff891c192 100644 --- a/R-package/R/callback.R +++ b/R-package/R/callback.R @@ -37,7 +37,12 @@ cb.reset.parameters <- function(new_params) { # Some parameters are not allowed to be changed, # since changing them would simply wreck some chaos - not_allowed <- c("num_class", "metric", "boosting_type") + not_allowed <- c( + "num_class" + , "metric" + , "boosting" + , .PARAMETER_ALIASES()[["boosting"]] + ) if (any(pnames %in% not_allowed)) { stop("Parameters ", paste0(pnames[pnames %in% not_allowed], collapse = ", "), " cannot be changed during boosting") } diff --git a/R-package/R/lgb.cv.R b/R-package/R/lgb.cv.R index 76626f5c4613..099fb642cc7f 100644 --- a/R-package/R/lgb.cv.R +++ b/R-package/R/lgb.cv.R @@ -224,8 +224,18 @@ lgb.cv <- function(params = list(), # Did user pass parameters that indicate they want to use early stopping? using_early_stopping_via_args <- !is.null(early_stopping_rounds) + boosting_param_names <- c("boosting", .PARAMETER_ALIASES()[["boosting"]]) + using_dart <- any( + sapply( + X = boosting_param_names + , FUN = function(param){ + identical(params[[param]], 'dart') + } + ) + ) + # Cannot use early stopping with 'dart' boosting - if (identical(params$boosting, "dart")){ + if (using_dart){ warning("Early stopping is not available in 'dart' mode.") using_early_stopping_via_args <- FALSE diff --git a/R-package/R/lgb.train.R b/R-package/R/lgb.train.R index 289c6fcdb40d..9ff8a72bfec3 100644 --- a/R-package/R/lgb.train.R +++ b/R-package/R/lgb.train.R @@ -207,8 +207,18 @@ lgb.train <- function(params = list(), # Did user pass parameters that indicate they want to use early stopping? using_early_stopping_via_args <- !is.null(early_stopping_rounds) + boosting_param_names <- c("boosting", .PARAMETER_ALIASES()[["boosting"]]) + using_dart <- any( + sapply( + X = boosting_param_names + , FUN = function(param){ + identical(params[[param]], 'dart') + } + ) + ) + # Cannot use early stopping with 'dart' boosting - if (identical(params$boosting, "dart")){ + if (using_dart){ warning("Early stopping is not available in 'dart' mode.") using_early_stopping_via_args <- FALSE diff --git a/R-package/tests/testthat/test_parameters.R b/R-package/tests/testthat/test_parameters.R index 8100deff59bb..51a5f95b8c4b 100644 --- a/R-package/tests/testthat/test_parameters.R +++ b/R-package/tests/testthat/test_parameters.R @@ -40,3 +40,30 @@ test_that("Feature penalties work properly", { # Ensure that feature is not used when feature_penalty = 0 expect_length(var_gain[[length(var_gain)]], 0) }) + +expect_true(".PARAMETER_ALIASES() returns a named list", { + param_aliases <- .PARAMETER_ALIASES() + expect_true(is.list(param_aliases)) + expect_true(is.character(names(param_aliases))) +}) + +expect_true("training should warn if you use 'dart' boosting, specified with 'boosting' or aliases", { + for (boosting_param in c("boosting", .PARAMETER_ALIASES()[["boosting"]])){ + expect_warning({ + result <- lightgbm( + data = train$data + , label = train$label + , num_leaves = 5 + , learning_rate = 0.05 + , nrounds = 5 + , objective = "binary" + , metric = "binary_error" + , verbose = -1 + , params = stats::setNames( + object = "dart" + , nm = boosting_param + ) + ) + }, regexp = "Early stopping is not available in 'dart' mode") + } +})