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] CVBooster$reset_parameter() is broken #4900

Closed
jameslamb opened this issue Dec 21, 2021 · 1 comment · Fixed by #4901
Closed

[R-package] CVBooster$reset_parameter() is broken #4900

jameslamb opened this issue Dec 21, 2021 · 1 comment · Fixed by #4901
Assignees

Comments

@jameslamb
Copy link
Collaborator

Description

lgb.cv() returns an object of class CVBooster. That class has a public method, $reset_parameter(), which is intended to be used to update the parameters of each Booster in CVBooster$boosters.

That method, which was added about 5 years ago (f3f2f5a), currently throws an error when called.

Error: object 'boosters' not found

Reproducible example

library(lightgbm)
data("agaricus.train")
dtrain <- lightgbm::lgb.Dataset(
    data = agaricus.train$data
    , label = agaricus.train$label
)
cv_bst <- lightgbm::lgb.cv(
    params = list(
        num_leaves = 8L
    )
    , nfold = 3L
    , nrounds = 10L
    , data = dtrain
    , obj = "binary"
    , verbose = -1L
)
cv_bst$reset_parameter(
    list(num_leaves = 11L)
)

Error in cv_bst$reset_parameter(list(num_leaves = 11L)) :
object 'boosters' not found

Environment info

LightGBM version or commit hash: v3.3.1.99 (7b10baf)

Command(s) you used to install LightGBM

sh build-cran-package.sh --no-build-vignettes && \
R CMD INSTALL ./lightgbm_3.3.1.99.tar.gz

Using R 4.1.0 on macOS.

Additional Comments

In the Python package, this works without issue.

import lightgbm as lgb
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=10_000, n_features=10, n_informative=5)

dtrain = lgb.Dataset(data=X, label=y)

results = lgb.cv(
    train_set=dtrain,
    params={
        "objective": "regression_l2",
        "num_leaves": 8,
        "verbose": -1
    },
    stratified=False,
    shuffle=False,
    return_cvbooster=True
)
cv_bst = results["cvbooster"]
for bst in cv_bst.boosters:
    print(bst.params)

# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}

_ = cv_bst.reset_parameter({"num_leaves": 11})
for bst in cv_bst.boosters:
    print(bst.params)

# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
# {'objective': 'regression_l2', 'num_leaves': 11, 'verbose': -1, 'num_iterations': 100, 'early_stopping_round': None}
@jameslamb jameslamb self-assigned this Dec 21, 2021
jameslamb added a commit that referenced this issue Dec 22, 2021
* [R-package] fix CVBooster reset_parameter() method (fixes #4900)

* make it clear that there should be one booster per fold
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
1 participant