Skip to content

[python-package] Model parameters are lost when loading Booster from model serialized string #6851

Closed
@Samsagax

Description

@Samsagax

Description

After training, saving the model on a string, then retrieving it, the params dictionary are lost

Reproducible example

import lightgbm as lgb
import numpy as np

# Train data
train_x = np.random.rand(1000, 20)
train_y = np.random.randint(0, 1, 1000)
train_data = lgb.Dataset(train_x, train_y)

# Parameters
params = {
    "boosting_type": "gbdt",
    "objective": "binary",
    "metric": "auc",
    "num_leaves": 31,
    "learning_rate": 0.05,
    "feature_fraction": 0.9,
    "bagging_fraction": 0.8,
    "bagging_freq": 5,
    "verbose": -100,
}

# train the model
model = lgb.train(params, train_data)

# print the params, should be the same as above
print(model.params)

# dump the model into a string
model_serialized = model.model_to_string()

# load a new model with the string
new_model = lgb.Booster(model_str=model_serialized)

# print the params, it returns an empty dict
print(new_model.params)

Environment info

LightGBM version or commit hash: 4.6.0

Command(s) you used to install LightGBM

$ pip install lightgbm

ArchLinux system

Additional Comments

I think the fix is pretty simple and stems from the __init__() function last line where the params member is overwritten with whatever is passed (empty, when loading from a serialized string)

I guess the fix is easy, will PR it in a moment.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions