Skip to content

Commit

Permalink
[R-package] update parameter 'verbosity' based on keyword arg 'verbos…
Browse files Browse the repository at this point in the history
…e' (#4899)

* [R-package] update parameter 'verbosity' based on keyword arg 'verbose'

* add tests
  • Loading branch information
jameslamb authored Dec 23, 2021
1 parent 2ef3cb8 commit cace5bb
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
6 changes: 5 additions & 1 deletion R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ lgb.cv <- function(params = list()
}

# Setup temporary variables
params$verbose <- verbose
params <- lgb.check.obj(params = params, obj = obj)
params <- lgb.check.eval(params = params, eval = eval)
fobj <- NULL
Expand All @@ -112,6 +111,11 @@ lgb.cv <- function(params = list()
# in `params`.
# this ensures that the model stored with Booster$save() correctly represents
# what was passed in
params <- lgb.check.wrapper_param(
main_param_name = "verbosity"
, params = params
, alternative_kwarg_value = verbose
)
params <- lgb.check.wrapper_param(
main_param_name = "num_iterations"
, params = params
Expand Down
6 changes: 5 additions & 1 deletion R-package/R/lgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ lgb.train <- function(params = list(),
}

# Setup temporary variables
params$verbose <- verbose
params <- lgb.check.obj(params = params, obj = obj)
params <- lgb.check.eval(params = params, eval = eval)
fobj <- NULL
Expand All @@ -84,6 +83,11 @@ lgb.train <- function(params = list(),
# in `params`.
# this ensures that the model stored with Booster$save() correctly represents
# what was passed in
params <- lgb.check.wrapper_param(
main_param_name = "verbosity"
, params = params
, alternative_kwarg_value = verbose
)
params <- lgb.check.wrapper_param(
main_param_name = "num_iterations"
, params = params
Expand Down
122 changes: 122 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,60 @@ test_that("lgb.train() works with integer, double, and numeric data", {
}
})

test_that("lgb.train() updates params based on keyword arguments", {
dtrain <- lgb.Dataset(
data = matrix(rnorm(400L), ncol = 4L)
, label = rnorm(100L)
)

# defaults from keyword arguments should be used if not specified in params
invisible(
capture.output({
bst <- lgb.train(
data = dtrain
, obj = "regression"
, params = list()
)
})
)
expect_equal(bst$params[["verbosity"]], 1L)
expect_equal(bst$params[["num_iterations"]], 100L)

# main param names should be preferred to keyword arguments
invisible(
capture.output({
bst <- lgb.train(
data = dtrain
, obj = "regression"
, params = list(
"verbosity" = 5L
, "num_iterations" = 2L
)
)
})
)
expect_equal(bst$params[["verbosity"]], 5L)
expect_equal(bst$params[["num_iterations"]], 2L)

# aliases should be preferred to keyword arguments, and converted to main parameter name
invisible(
capture.output({
bst <- lgb.train(
data = dtrain
, obj = "regression"
, params = list(
"verbose" = 5L
, "num_boost_round" = 2L
)
)
})
)
expect_equal(bst$params[["verbosity"]], 5L)
expect_false("verbose" %in% bst$params)
expect_equal(bst$params[["num_iterations"]], 2L)
expect_false("num_boost_round" %in% bst$params)
})

test_that("when early stopping is not activated, best_iter and best_score come from valids and not training data", {
set.seed(708L)
trainDF <- data.frame(
Expand Down Expand Up @@ -1978,6 +2032,74 @@ test_that("early stopping works with lgb.cv()", {
)
})

test_that("lgb.cv() updates params based on keyword arguments", {
dtrain <- lgb.Dataset(
data = matrix(rnorm(400L), ncol = 4L)
, label = rnorm(100L)
)

# defaults from keyword arguments should be used if not specified in params
invisible(
capture.output({
cv_bst <- lgb.cv(
data = dtrain
, obj = "regression"
, params = list()
, nfold = 2L
)
})
)

for (bst in cv_bst$boosters) {
bst_params <- bst[["booster"]]$params
expect_equal(bst_params[["verbosity"]], 1L)
expect_equal(bst_params[["num_iterations"]], 100L)
}

# main param names should be preferred to keyword arguments
invisible(
capture.output({
cv_bst <- lgb.cv(
data = dtrain
, obj = "regression"
, params = list(
"verbosity" = 5L
, "num_iterations" = 2L
)
, nfold = 2L
)
})
)
for (bst in cv_bst$boosters) {
bst_params <- bst[["booster"]]$params
expect_equal(bst_params[["verbosity"]], 5L)
expect_equal(bst_params[["num_iterations"]], 2L)
}

# aliases should be preferred to keyword arguments, and converted to main parameter name
invisible(
capture.output({
cv_bst <- lgb.cv(
data = dtrain
, obj = "regression"
, params = list(
"verbose" = 5L
, "num_boost_round" = 2L
)
, nfold = 2L
)
})
)
for (bst in cv_bst$boosters) {
bst_params <- bst[["booster"]]$params
expect_equal(bst_params[["verbosity"]], 5L)
expect_false("verbose" %in% bst_params)
expect_equal(bst_params[["num_iterations"]], 2L)
expect_false("num_boost_round" %in% bst_params)
}

})

context("linear learner")

test_that("lgb.train() fit on linearly-relatead data improves when using linear learners", {
Expand Down

0 comments on commit cace5bb

Please sign in to comment.