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

Unable to export code from XGBoost 1.7.5 models #581

Open
GuidoBartoli opened this issue May 30, 2023 · 6 comments
Open

Unable to export code from XGBoost 1.7.5 models #581

GuidoBartoli opened this issue May 30, 2023 · 6 comments

Comments

@GuidoBartoli
Copy link

I'm using the following code to generate Python code from a XGBoost (bst is a previously trained XGBoost object)

temp_file = "temp.json"
bst.save_model(temp_file)
skclf = xgb.XGBClassifier()
skclf.load_model(temp_file)
os.remove(temp_file)
skclf.n_classes_ = skclf.classes_ = classes
code = m2c.export_to_python(clf)

This worked fine with XGBoost 1.7.1, but when I updated to 1.7.5, I received the following error:

Traceback (most recent call last):
  File "/home/bartoli/Projects/aikit/boost.py", line 516, in <module>
    code = m2c.export_to_python(clf)
  File "/home/bartoli/miniconda3/envs/ai3/lib/python3.9/site-packages/m2cgen/exporters.py", line 57, in export_to_python
    return _export(model, interpreter)
  File "/home/bartoli/miniconda3/envs/ai3/lib/python3.9/site-packages/m2cgen/exporters.py", line 459, in _export
    model_ast = assembler_cls(model).assemble()
  File "/home/bartoli/miniconda3/envs/ai3/lib/python3.9/site-packages/m2cgen/assemblers/boosting.py", line 214, in assemble
    return self.assembler.assemble()
  File "/home/bartoli/miniconda3/envs/ai3/lib/python3.9/site-packages/m2cgen/assemblers/boosting.py", line 34, in assemble
    return self._assemble_bin_class_output(self._all_estimator_params)
  File "/home/bartoli/miniconda3/envs/ai3/lib/python3.9/site-packages/m2cgen/assemblers/boosting.py", line 80, in _assemble_bin_class_output
    base_score = -math.log(1.0 / self._base_score - 1.0)
TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'

Does m2cgen have support for the latest XGBoost version or do I have something to tweak inside the model to make it work like before?

Thanks

@DonnieFy
Copy link

meet the same error

@BennyH26
Copy link

Encountering the same error. This is a breaking change for my pipeline. Any ideas here?

@GuidoBartoli
Copy link
Author

Encountering the same error. This is a breaking change for my pipeline. Any ideas here?

Yep, it is breaking for me too, I hope for some updates from the developer...

@GuidoBartoli
Copy link
Author

GuidoBartoli commented Jun 16, 2023

Taking a look at m2cgen/assemblers/boosting.py (line 78:80), I think the problem depends on self._base_score being None for some reason in the latest XGBoost model version, so the check if self._base_score != 0.0 is passed and the next instruction fails base_score = -math.log(1.0 / self._base_score - 1.0).

Maybe this model field has been renamed or removed, I will check it out and update this issue.

@GuidoBartoli
Copy link
Author

GuidoBartoli commented Jun 29, 2023

Debugging into m2cgen code, maybe I managed to find a workaround for my case (export a native Booster to C and Python code), but I do not know if it works for other cases.

If the base_score parameter of the classifier is forced to 0 (the default value in the latest version is None), the check is passed and both Python and C code are generated.

# booster is already trained
temp_file = "temp.ubj"
booster.save_model(temp_file)
xgbclf = xgb.XGBClassifier()
xgbclf.load_model(temp_file)
os.remove(temp_file)
xgbclf.base_score = 0  # workaround
c_code = m2c.export_to_c(xgbclf)
py_code = m2c.export_to_python(xgbclf)

@wyitong
Copy link

wyitong commented Dec 27, 2023

Debugging into m2cgen code, maybe I managed to find a workaround for my case (export a native Booster to C and Python code), but I do not know if it works for other cases.

If the base_score parameter of the classifier is forced to 0 (the default value in the latest version is None), the check is passed and both Python and C code are generated.

# booster is already trained
temp_file = "temp.ubj"
booster.save_model(temp_file)
xgbclf = xgb.XGBClassifier()
xgbclf.load_model(temp_file)
os.remove(temp_file)
xgbclf.base_score = 0  # workaround
c_code = m2c.export_to_c(xgbclf)
py_code = m2c.export_to_python(xgbclf)

This works for me. Thanks!

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

No branches or pull requests

4 participants