Skip to content

Commit

Permalink
[R-package] fix CVBooster reset_parameter() method (fixes #4900) (#4901)
Browse files Browse the repository at this point in the history
* [R-package] fix CVBooster reset_parameter() method (fixes #4900)

* make it clear that there should be one booster per fold
  • Loading branch information
jameslamb authored Dec 22, 2021
1 parent 0df3810 commit 34b7484
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ CVBooster <- R6::R6Class(
return(invisible(NULL))
},
reset_parameter = function(new_params) {
for (x in boosters) {
x$reset_parameter(params = new_params)
for (x in self$boosters) {
x[["booster"]]$reset_parameter(params = new_params)
}
return(invisible(self))
}
Expand Down
25 changes: 25 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,31 @@ test_that("cv works", {
expect_false(is.null(bst$record_evals))
})

test_that("CVBooster$reset_parameter() works as expected", {
dtrain <- lgb.Dataset(train$data, label = train$label)
n_folds <- 2L
cv_bst <- lgb.cv(
params = list(
objective = "regression"
, min_data = 1L
, num_leaves = 7L
, verbose = VERBOSITY
)
, data = dtrain
, nrounds = 3L
, nfold = n_folds
)
expect_is(cv_bst, "lgb.CVBooster")
expect_length(cv_bst$boosters, n_folds)
for (bst in cv_bst$boosters) {
expect_equal(bst[["booster"]]$params[["num_leaves"]], 7L)
}
cv_bst$reset_parameter(list(num_leaves = 11L))
for (bst in cv_bst$boosters) {
expect_equal(bst[["booster"]]$params[["num_leaves"]], 11L)
}
})

test_that("lgb.cv() rejects negative or 0 value passed to nrounds", {
dtrain <- lgb.Dataset(train$data, label = train$label)
params <- list(
Expand Down

0 comments on commit 34b7484

Please sign in to comment.