-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Load back parameters when loading Booster from lgb.cv() saved to text file? #4883
Comments
Thanks for opening a new issue. Can you please add more details for what you are trying to do? A reproducible example with an explanation of what you expected would be very helpful. |
@acmilannesta Thanks for using LightGBM. Could you tell us how did you save and reload the boosters? That would be helpful for us. |
I basically call the I learned that by passing |
Ok thanks @acmilannesta , I think I understand (although a reproducible example would eliminate the need to guess).
I think the code below captures the behavior you're talking about. import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.datasets import make_regression
X, y = make_regression(n_samples=4_000, n_informative=10)
params = {
'objective': 'regression_l2',
'learning_rate': 0.123,
'min_data_in_leaf': 3,
'verbose': -1
}
dtrain = lgb.Dataset(data=X, label=y)
cv_results = lgb.cv(
params=params,
train_set=dtrain,
return_cvbooster=True,
nfold=2,
stratified=False,
shuffle=False,
)
cv_booster = cv_results["cvbooster"]
# check parameters of each Booster
for bst in cv_booster.boosters:
print(bst.params)
# {'objective': 'regression_l2', 'learning_rate': 0.123, 'min_data_in_leaf': 3, 'verbose': -1, 'num_iterations': 100}
# {'objective': 'regression_l2', 'learning_rate': 0.123, 'min_data_in_leaf': 3, 'verbose': -1, 'num_iterations': 100}
# save one of the booster and reload it
cv_booster.boosters[0].save_model("first-booster.txt")
loaded_booster = lgb.Booster(
model_file="first-booster.txt"
)
print(loaded_booster.params)
# {} However, I don't believe the statement "by passing # train a single model and save it to file
bst = lgb.train(
params=params,
train_set=dtrain,
keep_training_booster=True
)
bst.save_model("lgb-train.txt")
# re-load that model and check params
loaded_booster = lgb.Booster(model_file="lgb-train.txt")
loaded_booster.params
# {} #2613 documents the feature request "populate Given all that....please subscribe to #2613 and #3556 for notifications about improvements to saving and loading Until those updates are made, I think you could achieve the behavior you want with one of the following approaches:
|
keep_training_booster = True
in lgbm.cv
function?
Thank you for the detailed explanations! I think the |
Great! Sorry for the inconvenience. Hopefully #2613 will be resolved in one of the next few releases. |
This issue has been automatically locked since there has not been any recent activity since it was closed. |
Could I also specify
keep_training_booster = True
inlgbm.cv
function?When I save returned cv booster from each fold and reload later, the
booster.param
will return an empty dictionary.Originally posted by @acmilannesta in #1364 (comment)
The text was updated successfully, but these errors were encountered: