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

[python-package] using lgb.Booster(model_file='model.txt') hangs when loading gbdt regression model #6513

Open
myron0330 opened this issue Jul 2, 2024 · 8 comments

Comments

@myron0330
Copy link

I setup a dev environment at ubuntu 20, gcc v9, python 3.9. and install the lightgbm==4.1.0.

when I try to load my model, it hangs from the beginning, can not be interupted and no error being throwed.
I also tried a mini-case as follows, it can not work either. I want to know that is there anything else to do upon training and using a gbdt regression model? How can i debug this problem? Is it related to gcc stuff?

import numpy as np
import lightgbm as lgb
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

# 生成一些示例数据
X, y = make_regression(n_samples=100, n_features=10, noise=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建数据集
train_data = lgb.Dataset(X_train, label=y_train)

# 设置参数
params = {
    'objective': 'regression',
    'metric': 'rmse',
    'boosting_type': 'gbdt'
}

# 训练模型
bst = lgb.train(params, train_data, num_boost_round=10)

# 保存模型
model_path = 'small_model.txt'
bst.save_model(model_path)

# 加载模型
try:
    bst_loaded = lgb.Booster(model_file=model_path)
    print("Model loaded successfully.")
except Exception as e:
    print(f"Error loading model: {e}")
@myron0330
Copy link
Author

It hungs at

bst = lgb.train(params, train_data, num_boost_round=10)
@jameslamb
Copy link
Collaborator

Thanks for using LightGBM.

If it hands on lgb.train(), then I guess the code after that line is not necessary, correct? If so, please remove it from the example.

Can you please share any logs produced by lightgbm prior to it "hanging"?

@jameslamb jameslamb changed the title using lgb.Booster(model_file='model.txt') hangs when loading gbdt regression model Jul 2, 2024
@myron0330
Copy link
Author

ok, thanks for your help. In fact, i had a model file and i just wanted to load it at beginning. It hanged at "lgb.Booster(model_file=model_path)" when loading, after that i tried the mini-case and it hanged at "lgb.train()".

By the way, what's the default log path by lightgbm? I will try to find more information tonight~

@jameslamb
Copy link
Collaborator

By the way, what's the default log path by lightgbm?

By default, lightgbm does not write logs to a file. It writes them directly to standard output (for example, printed in the terminal or the output below an executing Jupyter notebook cell).

@myron0330
Copy link
Author

it hangs at after print(1), at code _safe_call(_LIB.LGBM_BoosterCreateFromModelfile( _c_str(str(model_file)), ctypes.byref(out_num_iterations), ctypes.byref(self._handle)))

        elif model_file is not None:
            # Prediction task
            out_num_iterations = ctypes.c_int(0)
            print(1)
            _safe_call(_LIB.LGBM_BoosterCreateFromModelfile(
                _c_str(str(model_file)),
                ctypes.byref(out_num_iterations),
                ctypes.byref(self._handle)))
            print(2)
            out_num_class = ctypes.c_int(0)
            _safe_call(_LIB.LGBM_BoosterGetNumClasses(
                self._handle,
                ctypes.byref(out_num_class)))
            print(3)
            self.__num_class = out_num_class.value
            print(4)
            self.pandas_categorical = _load_pandas_categorical(file_name=model_file)
            print(5)
            if params:
                _log_warning('Ignoring params argument, using parameters from model file.')
            params = self._get_loaded_param()
            print(6)
@myron0330
Copy link
Author

ok, I find the problem. It's because of my numpy, scipy, pandas version is not compatible to the model file.

should make sure that these compute libs are compatible to the model files.

@myron0330
Copy link
Author

maybe it's better for the team to maintain the conda installation with higher versions.

@jameslamb
Copy link
Collaborator

ok, I find the problem. It's because of my numpy, scipy, pandas version is not compatible to the model file.

Can you please provide specific details about this? I don't understand this statement.

You may want to see "How to create a reproducible example" (link) for some advice on reporting software problems in a way that others can help confirm and investigate them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment