Skip to content

Commit

Permalink
added support for boosting aliases in R package and test on skipping …
Browse files Browse the repository at this point in the history
…early boosting
  • Loading branch information
jameslamb committed Oct 24, 2019
1 parent ae7cc57 commit e48cdf3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
15 changes: 15 additions & 0 deletions R-package/R/aliases.R
Original file line number Diff line number Diff line change
@@ -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"
)
))
}
7 changes: 6 additions & 1 deletion R-package/R/callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
12 changes: 11 additions & 1 deletion R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion R-package/R/lgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions R-package/tests/testthat/test_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})

0 comments on commit e48cdf3

Please sign in to comment.