-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[python-package] params
are lost when copying a Booster
#5539
Comments
params
if copied.params
are lost when copying a Booster
Thanks for this report! I agree that it's desirable for the new Booster's Here's the relevant code run during such a copy. LightGBM/python-package/lightgbm/basic.py Lines 2841 to 2847 in 0c0eb2a
One approach that might be taken to solve this is to mimic what's done when pickling / unpickling boosters, where every attribute except LightGBM/python-package/lightgbm/basic.py Lines 2849 to 2856 in 0c0eb2a
But I'd have to look into it more closely...there are several ways to solve this, I think. @hsorsky are you interested in attempting a pull request? If not, no worries and we're thankful for the report...but wanted to give you the first opportunity. |
Yes, I'm interested in attempting, but I might need some guidance. I assume it's preferable to have If def __deepcopy__(self, _):
model_str = self.model_to_string(num_iteration=-1)
booster = Booster(model_str=model_str)
if not booster.params:
booster.params = deepcopy(self.params)
return booster work? If not, and it would be required to be in |
With #5424 merged we can actually get all the params back from the model string, might be something to consider, i.e. import lightgbm as lgb
import numpy as np
ds = lgb.Dataset(np.random.rand(100, 2))
bst = lgb.train({'num_leaves': 5, 'verbosity': -1}, ds)
bst2 = lgb.Booster(model_str=bst.model_to_string())
print(bst2._get_loaded_param()) returns all the parameters. While working on that I remember at one moment I added it somewhere to model_from_string as well and these lines LightGBM/python-package/lightgbm/engine.py Lines 267 to 268 in 0c0eb2a
made it so that the booster that is returned from the train function had all the parameters. |
With these changes: --- a/python-package/lightgbm/basic.py
+++ b/python-package/lightgbm/basic.py
@@ -2802,6 +2802,7 @@ class Booster:
self.__get_eval_info()
self.pandas_categorical = train_set.pandas_categorical
self.train_set_version = train_set.version
+ self.params = params
elif model_file is not None:
# Prediction task
out_num_iterations = ctypes.c_int(0)
@@ -2818,13 +2819,12 @@ class Booster:
self.pandas_categorical = _load_pandas_categorical(file_name=model_file)
if params:
_log_warning('Ignoring params argument, using parameters from model file.')
- params = self._get_loaded_param()
+ self.params = self._get_loaded_param()
elif model_str is not None:
self.model_from_string(model_str)
else:
raise TypeError('Need at least one training dataset or model file or model string '
'to create Booster instance')
- self.params = params
def __del__(self) -> None:
try:
@@ -3582,6 +3582,7 @@ class Booster:
ctypes.byref(out_num_class)))
self.__num_class = out_num_class.value
self.pandas_categorical = _load_pandas_categorical(model_str=model_str)
+ self.params = self._get_loaded_param()
return self We'd get all parameters back from the train function and when copying. import copy
import lightgbm as lgb
import numpy as np
ds = lgb.Dataset(np.random.rand(100, 2))
bst = lgb.train({'num_leaves': 5, 'verbosity': -1}, ds)
bst2 = copy.deepcopy(bst)
print(len(bst.params)) # 90
print(len(bst2.params)) # 90 |
ok yeah nice! At first glance, I like that (@jmoralez 's suggested approach above) I'm going to be traveling for the next few days and might not be available much. So @hsorsky if you put up a pull request (with tests please 😀 ) I might not be able to look at it until next week, but hopefully @jmoralez will be able to when time permits. |
👋🏻 I'm working on a fix for this right now. |
@jameslamb I've just encountered it too, happy to assist with the PR if too busy (: |
Description
A copied
Booster
loses it'sparams
attributeReproducible example
Environment info
LightGBM version or commit hash:
3.3.2
Command(s) you used to install LightGBM
Additional Comments
Perhaps
Booster.model_from_string
should set it after initialising the object?The text was updated successfully, but these errors were encountered: