Skip to content

Commit

Permalink
[R-package] only warn about early stopping and DART boosting being in…
Browse files Browse the repository at this point in the history
…compatible if early stopping was requested (#6619)
  • Loading branch information
serkor1 committed Aug 21, 2024
1 parent d67ecf9 commit 5fa615b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
4 changes: 3 additions & 1 deletion R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ lgb.cv <- function(params = list()

# Cannot use early stopping with 'dart' boosting
if (using_dart) {
warning("Early stopping is not available in 'dart' mode.")
if (using_early_stopping) {
warning("Early stopping is not available in 'dart' mode.")
}
using_early_stopping <- FALSE

# Remove the cb_early_stop() function if it was passed in to callbacks
Expand Down
4 changes: 3 additions & 1 deletion R-package/R/lgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ lgb.train <- function(params = list(),

# Cannot use early stopping with 'dart' boosting
if (using_dart) {
warning("Early stopping is not available in 'dart' mode.")
if (using_early_stopping) {
warning("Early stopping is not available in 'dart' mode.")
}
using_early_stopping <- FALSE

# Remove the cb_early_stop() function if it was passed in to callbacks
Expand Down
61 changes: 58 additions & 3 deletions R-package/tests/testthat/test_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test_that(".PARAMETER_ALIASES() uses the internal session cache", {
expect_false(exists(cache_key, where = .lgb_session_cache_env))
})

test_that("training should warn if you use 'dart' boosting, specified with 'boosting' or aliases", {
test_that("training should warn if you use 'dart' boosting with early stopping", {
for (boosting_param in .PARAMETER_ALIASES()[["boosting"]]) {
params <- list(
num_leaves = 5L
Expand All @@ -101,14 +101,69 @@ test_that("training should warn if you use 'dart' boosting, specified with 'boos
, num_threads = .LGB_MAX_THREADS
)
params[[boosting_param]] <- "dart"

# warning: early stopping requested
expect_warning({
result <- lightgbm(
data = train$data
, label = train$label
, params = params
, nrounds = 5L
, verbose = -1L
, nrounds = 2L
, verbose = .LGB_VERBOSITY
, early_stopping_rounds = 1L
)
}, regexp = "Early stopping is not available in 'dart' mode")

# no warning: early stopping not requested
expect_silent({
result <- lightgbm(
data = train$data
, label = train$label
, params = params
, nrounds = 2L
, verbose = .LGB_VERBOSITY
, early_stopping_rounds = NULL
)
})
}
})

test_that("lgb.cv() should warn if you use 'dart' boosting with early stopping", {
for (boosting_param in .PARAMETER_ALIASES()[["boosting"]]) {
params <- list(
num_leaves = 5L
, objective = "binary"
, metric = "binary_error"
, num_threads = .LGB_MAX_THREADS
)
params[[boosting_param]] <- "dart"

# warning: early stopping requested
expect_warning({
result <- lgb.cv(
data = lgb.Dataset(
data = train$data
, label = train$label
)
, params = params
, nrounds = 2L
, verbose = .LGB_VERBOSITY
, early_stopping_rounds = 1L
)
}, regexp = "Early stopping is not available in 'dart' mode")

# no warning: early stopping not requested
expect_silent({
result <- lgb.cv(
data = lgb.Dataset(
data = train$data
, label = train$label
)
, params = params
, nrounds = 2L
, verbose = .LGB_VERBOSITY
, early_stopping_rounds = NULL
)
})
}
})

0 comments on commit 5fa615b

Please sign in to comment.