-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing dependency onnxconverter_common, fix multi regression wit…
…h xgboost (#679) * add missing dependency onnxconverter_common Signed-off-by: Xavier Dupre <[email protected]> * issue 676 Signed-off-by: Xavier Dupre <[email protected]> * issue 676 Signed-off-by: Xavier Dupre <[email protected]> * fix issue 676 Signed-off-by: Xavier Dupre <[email protected]> * fix shape calcultator Signed-off-by: Xavier Dupre <[email protected]> * fix new name for sparse arguement Signed-off-by: Xavier Dupre <[email protected]> * changelogs Signed-off-by: Xavier Dupre <[email protected]> * improves stability Signed-off-by: Xavier Dupre <[email protected]> --------- Signed-off-by: Xavier Dupre <[email protected]>
- Loading branch information
Showing
8 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
numpy | ||
onnx | ||
onnxconverter_common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import unittest | ||
|
||
|
||
class TestXGBoostIssues(unittest.TestCase): | ||
def test_issue_676(self): | ||
import json | ||
import onnxruntime | ||
import xgboost | ||
import numpy as np | ||
from skl2onnx import convert_sklearn | ||
from skl2onnx.common.data_types import FloatTensorType | ||
from skl2onnx import update_registered_converter | ||
from onnxmltools.convert.xgboost.operator_converters.XGBoost import ( | ||
convert_xgboost, | ||
) | ||
|
||
def xgbregressor_shape_calculator(operator): | ||
config = json.loads(operator.raw_operator.get_booster().save_config()) | ||
n_targets = int(config["learner"]["learner_model_param"]["num_target"]) | ||
operator.outputs[0].type.shape = [None, n_targets] | ||
|
||
update_registered_converter( | ||
xgboost.XGBRegressor, | ||
"XGBoostXGBRegressor", | ||
xgbregressor_shape_calculator, | ||
convert_xgboost, | ||
) | ||
# Your data and labels | ||
X = np.random.rand(100, 10) | ||
y = np.random.rand(100, 2) | ||
|
||
# Train XGBoost regressor | ||
model = xgboost.XGBRegressor( | ||
objective="reg:squarederror", n_estimators=2, maxdepth=2 | ||
) | ||
model.fit(X, y) | ||
|
||
# Define input type (adjust shape according to your input) | ||
initial_type = [("float_input", FloatTensorType([None, X.shape[1]]))] | ||
|
||
# Convert XGBoost model to ONNX | ||
onnx_model = convert_sklearn(model, initial_types=initial_type, target_opset=12) | ||
self.assertIn("dim_value: 2", str(onnx_model.graph.output)) | ||
|
||
sess = onnxruntime.InferenceSession( | ||
onnx_model.SerializeToString(), providers=["CPUExecutionProvider"] | ||
) | ||
got = sess.run(None, {"float_input": X.astype(np.float32)}) | ||
self.assertEqual(got[0].shape, (100, 2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters