Skip to content

Commit

Permalink
Fix discrepencies when xgboost trees are empty. (#447)
Browse files Browse the repository at this point in the history
* Fix discrepencies when xgboost trees are empty.

Signed-off-by: xavier dupré <xavier.dupre@gmail.com>

* fix wrong converting function

Signed-off-by: xavier dupré <xavier.dupre@gmail.com>

* eol

Signed-off-by: xavier dupré <xavier.dupre@gmail.com>

Co-authored-by: xavier dupré <xavier.dupre@gmail.com>
  • Loading branch information
xadupre and sdpython authored Feb 19, 2021
1 parent ccddab5 commit 331df2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions onnxmltools/convert/xgboost/operator_converters/XGBoost.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def convert(scope, operator, container):
# See https://github.com/dmlc/xgboost/blob/master/src/common/math.h#L23.
attr_pairs['post_transform'] = "LOGISTIC"
attr_pairs['class_ids'] = [0 for v in attr_pairs['class_treeids']]
if js_trees[0].get('leaf', None) == 0:
attr_pairs['base_values'] = [0.5]
else:
# See https://github.com/dmlc/xgboost/blob/master/src/common/math.h#L35.
attr_pairs['post_transform'] = "SOFTMAX"
Expand Down
17 changes: 17 additions & 0 deletions tests/xgboost/test_xgboost_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import unittest
import numpy as np
from numpy.testing import assert_almost_equal
import pandas
from sklearn.datasets import (
load_diabetes, load_iris, make_classification, load_digits)
Expand Down Expand Up @@ -325,6 +326,22 @@ def test_xgboost_example_mnist(self):
allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')",
basename="XGBoostExample")

def test_xgb_empty_tree(self):
xgb = XGBClassifier(n_estimators=2, max_depth=2)

# simple dataset
X = [[0, 1], [1, 1], [2, 0]]
X = np.array(X, dtype=np.float32)
y = [0, 1, 0]
xgb.fit(X, y)
conv_model = convert_xgboost(
xgb, initial_types=[
('input', FloatTensorType(shape=[None, X.shape[1]]))])
sess = InferenceSession(conv_model.SerializeToString())
res = sess.run(None, {'input': X.astype(np.float32)})
assert_almost_equal(xgb.predict_proba(X), res[1])
assert_almost_equal(xgb.predict(X), res[0])


if __name__ == "__main__":
unittest.main()

0 comments on commit 331df2e

Please sign in to comment.