Skip to content

Conversation

@lukaswenzl
Copy link

Currently, when creating a xgboost model with at least one of the features being a categorical feature (using enable_categorical=True) the conversion to onnx fails.
In this pr, I am proposing to add support for converting categorical features in a xgboost model similarly as is done for lightgbm where the conversion works already.

Context: Usually, the decision trees of a GBM model use numeric comparisons (e.g. go left if feature < x). However, categorical features split based on set operations (e.g. go left if feature has one of categories [A, B, D]).

For xgboost these set comparisons are implemented as a list of categories in the "split_condition" field instead of a number for the numeric comparison.
The TreeEnsemble operator in onnx does not directly (currently) support set operations, but it does support equality checks.
The set operation can be represented in the TreeEnsemble operator as multiple equality checks, using additional branches in the decision tree.

Example 1: the split condition go left (id 1) if feature is one of categories [A] otherwise go right (id 2) can easily represented as:
id 0: go to id 1 if feature == A, else go to id 2

Example 2: the split condition go left (id 1) if feature is one of categories [A, B] otherwise go right (id 2) can be represented as:
id 0: go to id 1 if feature == A, else go to id 3
id 3: go to copy of id 1 if feature == B, else go to id 2

Some edge cases need to be accounted for: left and right can be flipped; the behavior when input is missing also needs to be handled.

In this pr proposing an implementation of this conversion for xgboost models. I added unit tests to check that this conversion works correctly for a number of different xgboost setups.

Notes:

  • the inference performance of the converted model is less optimal for many categories as many separate equality checks are needed. This is the same performance limitation as discussed here for lightgbm: Inference performance issues with Lightgbm when using categoricals  #647 To improve this better categorical support would be needed in the onnx definition of TreeEnsembleRegressor /TreeEnsembleClassifier
  • xgboost recently introduced support for auto-reencoding of categories to codes. See https://xgboost.readthedocs.io/en/stable/tutorials/categorical.html
    This is a similar feature to what lightgbm can do when using pandas. Neither of these can be used for the onnxmlconverter in its current form as it currently only supports numeric float inputs. So this change has no effect here. The onnxmlconverter will only use the final category codes. To run inference with the onnx file the category codes need to be used as inputs.
  • Version 3.1.0 of xgboost introduced a breaking changed. I proposed a fix here: Fix intercept handling of xgboost starting with version 3.1.0 #733 it needs to be merged before the tests here can pass.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant