Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1534,6 +1534,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(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these uses of invisible(capture.output(...)) suppress log messages from being printed, per #4862

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 @@ -1953,6 +2007,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