onnx / sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Apache License 2.0
542 stars 99 forks source link

ValueError: Unable to create node 'TreeEnsembleClassifier' with name='TreeEnsembleClassifier'. #807

Closed z7ye closed 1 year ago

z7ye commented 2 years ago
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification, make_regression
import shutil
import xgboost as xgb
import onnx
import onnxruntime as rt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from skl2onnx.common.shape_calculator import (
calculate_linear_classifier_output_shapes,
calculate_linear_regressor_output_shapes,
)
data = load_iris()
X_iris = data.data[:, :2]
y_iris = data.target
ind = np.arange(cls.X_iris.shape[0])
np.random.shuffle(ind)
X_iris = cls.X_iris[ind, :].copy()
y_iris = cls.y_iris[ind].copy()
pipe = Pipeline([('scaler', StandardScaler()),
                ('xgbc', XGBClassifier(n_estimators=3))])

update_registered_converter(
                  xgb.XGBClassifier,
                  "XGBoostXGBClassifier",
                  calculate_linear_classifier_output_shapes,
                  convert_xgboost,
                  options={"nocl": [True, False], "zipmap": [True, False]},
              )

to_onnx(pipe)

gives this error ValueError: Unable to create node 'TreeEnsembleClassifier' with name='TreeEnsembleClassifier'.

xadupre commented 2 years ago

I could not replicate your issue. I updated your example to make it work. The main issues were the fact the model was not trained before being converted and the second argument missing in to_onnx which tells the converting library the input type of the model.

import numpy
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification, make_regression
import shutil
import xgboost as xgb
import onnx
import onnxruntime as rt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from skl2onnx.common.shape_calculator import (
calculate_linear_classifier_output_shapes,
calculate_linear_regressor_output_shapes,
)
from skl2onnx import to_onnx, update_registered_converter
from onnxmltools.convert.xgboost.operator_converters.XGBoost import (
    convert_xgboost)

data = load_iris()
X_iris = data.data[:, :2]
y_iris = data.target
ind = np.arange(X_iris.shape[0])
np.random.shuffle(ind)
X_iris = X_iris[ind, :].copy()
y_iris = y_iris[ind].copy()
pipe = Pipeline([('scaler', StandardScaler()),
                ('xgbc', XGBClassifier(n_estimators=3))])
pipe.fit(X_iris, y_iris)

update_registered_converter(
                  xgb.XGBClassifier,
                  "XGBoostXGBClassifier",
                  calculate_linear_classifier_output_shapes,
                  convert_xgboost,
                  options={"nocl": [True, False], "zipmap": [True, False]},
              )

to_onnx(pipe, X_iris[:1].astype(numpy.float32))
xiaowuhu commented 2 years ago

@z7ye Hi, we will close this issue if you don't have any concern.