Here's a filled out issue report:
Description
There appears to be an inconsistency in how parameters are handled when loading a model from string versus loading from file. When using model_from_string(), the .params attribute is not set, while it is properly set when loading from a file using the same model. This creates inconsistent behavior in the API.
Reproducible example
import lightgbm as lgb
import numpy as np
# Create a simple model first
X = np.random.rand(100, 4)
y = np.random.rand(100)
train_data = lgb.Dataset(X, label=y)
model = lgb.train({'objective': 'regression'}, train_data, num_boost_round=1)
# Save model to string
model_str = model.model_to_string()
# Load model from string
loaded_model_str = lgb.Booster(model_str=model_str)
print("Params when loaded from string:", loaded_model_str.params) # Empty dict {}
print("Params using _get_loaded_param():", loaded_model_str._get_loaded_param()) # Shows parameters
# Save and load from file for comparison
model.save_model('temp.txt')
loaded_model_file = lgb.Booster(model_file='temp.txt')
print("Params when loaded from file:", loaded_model_file.params) # Shows parameters
Environment info
LightGBM version or commit hash: 4.5.0
Additional Comments
Looking at the source code, this appears to be due to model_from_string() not setting the params attribute, while loading from file does set it via self._get_loaded_param(). This inconsistency could cause issues for users relying on the .params attribute to access model parameters, especially with libraries like shap
The fix would likely involve adding self.params = self._get_loaded_param() in the model_from_string() method to maintain consistency with file loading behavior.
Relevant code sections showing the inconsistency:
- When loading from file:
params = self._get_loaded_param()
- When loading from string: no parameter setting occurs
This behavior difference doesn't seem intentional and could be considered a minor bug in the API design.
Here's a filled out issue report:
Description
There appears to be an inconsistency in how parameters are handled when loading a model from string versus loading from file. When using
model_from_string(), the.paramsattribute is not set, while it is properly set when loading from a file using the same model. This creates inconsistent behavior in the API.Reproducible example
Environment info
LightGBM version or commit hash: 4.5.0
Additional Comments
Looking at the source code, this appears to be due to
model_from_string()not setting theparamsattribute, while loading from file does set it viaself._get_loaded_param(). This inconsistency could cause issues for users relying on the.paramsattribute to access model parameters, especially with libraries likeshapThe fix would likely involve adding
self.params = self._get_loaded_param()in themodel_from_string()method to maintain consistency with file loading behavior.Relevant code sections showing the inconsistency:
params = self._get_loaded_param()This behavior difference doesn't seem intentional and could be considered a minor bug in the API design.