scikit-learn-intelex icon indicating copy to clipboard operation
scikit-learn-intelex copied to clipboard

Can't convert LGBMClassifier with daal4py

Open GLmontanari opened this issue 1 year ago • 1 comments

Describe the bug I am trying to accelerate a LGBMClassifier model on CPU but I can't. I have tried different ways:

To Reproduce This is my code

import lightgbm as lgb
import daal4py as d4p

# Then I have a section where I load the data
# ...

# This is not working
lgb_model = lgb.LGBMClassifier(num_leaves=31).fit(x_train, y_train)
daal_model = d4p.get_gbt_model_from_lightgbm(lgb_model)  # AttributeError: 'LGBMClassifier' object has no attribute 'dump_model'

# I don't know the difference (yet), but this is working
lgb_model = lgb.train({'num_leaves': 31}, lgb.Dataset(x_train, y_train))
daal_model = d4p.get_gbt_model_from_lightgbm(lgb_model)

# Unfortunately, when I test on the test set, this method returns
# 1: float labels instead of integers (I want to do multiclass classification)
# 2: the results are different from the non-optimized LGBMClassifier, in particular they are worse

Output/Screenshots Upon converting the model with this line d4p.get_gbt_model_from_lightgbm(lgb_model) I get an exception:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1535, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\MiniProgetti\SEA_ML_BENCHMARKS\sklearn_benchmarks\TestIntel.py", line 90, in <module>
    d4p.get_gbt_model_from_lightgbm(lgb_model)  # AttributeError: 'LGBMClassifier' object has no attribute 'dump_model'
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "build\\daal4py_cy.pyx", line 21558, in daal4py._daal4py.get_gbt_model_from_lightgbm
AttributeError: 'LGBMClassifier' object has no attribute 'dump_model'

I have tried also daal4py 2023.2.0 and daal4py 2023.2.1 and get a different exception:

Traceback (most recent call last):
  File "C:\Users\iicgym\AppData\Local\Programs\Python\Python311\Lib\timeit.py", line 180, in timeit
    timing = self.inner(it, self.timer)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<timeit-src>", line 7, in inner
  File "build\\daal4py_cy.pyx", line 21576, in daal4py._daal4py.get_gbt_model_from_lightgbm
  File "build\\daal4py_cy.pyx", line 21445, in daal4py._daal4py.TreeList.from_lightgbm_booster_dump
  File "build\\daal4py_cy.pyx", line 21353, in daal4py._daal4py.Node.from_lightgbm_dict
KeyError: 'leaf_count'
python-BaseException

Environment:

  • OS: Windows10
  • Python 3.11.9
  • daal4py 2024.5.0
  • sklearn-intelex 2024.5.0
  • lightgbm 4.5.0 (tried also 4.3.0)

GLmontanari avatar Aug 20 '24 10:08 GLmontanari