Search code examples
pythonazureazure-machine-learning-service

XGBClassifer, when de-serialized, gives 'XGBModel' object has no attribute 'enable_categorical'


I have a serialized XGBClassifier object, trained and generated using xgboost=1.5.2.

XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=0.5,
              colsample_bynode=1, colsample_bytree=0.30140958911801474,
              eval_metric='logloss', gamma=0.1203484640861413, gpu_id=-1,
              importance_type='gain', interaction_constraints='',
              learning_rate=0.1, max_bin=368, max_delta_step=0, max_depth=6,
              min_child_weight=1, missing=nan, monotone_constraints='()',
              n_estimators=100, n_jobs=6, num_parallel_tree=1, random_state=42,
              reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
              single_precision_histogram=True, subsample=0.976171515775659,
              tree_method='gpu_hist', use_label_encoder=False,
              validate_parameters=1, verbosity=None)

I load the object using:

clf_model = joblib.load(model_path)

I want to use the object to predict on some data I am using Azure environment which also has xgboost=1.5.2. but it gives error:

File "score.py", line 78, in score_execution
[stderr]    clf_preds = clf_model.predict(clf_data_transformed)
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/xgboost/sklearn.py", line 1284, in predict
[stderr]    class_probs = super().predict(
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/xgboost/sklearn.py", line 879, in predict
[stderr]    if self._can_use_inplace_predict():
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/xgboost/sklearn.py", line 811, in _can_use_inplace_predict
[stderr]    predictor = self.get_params().get("predictor", None)
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/xgboost/sklearn.py", line 505, in get_params
[stderr]    params.update(cp.__class__.get_params(cp, deep))
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/xgboost/sklearn.py", line 502, in get_params
[stderr]    params = super().get_params(deep)
[stderr]  File "/opt/miniconda/lib/python3.8/site-packages/sklearn/base.py", line 210, in get_params
[stderr]    value = getattr(self, key)
[stderr]AttributeError: 'XGBModel' object has no attribute 'enable_categorical'

We have same version in pipelines that produce/serialize the model and in the pipeline that deserialize the model to predict on new data.


Solution

  • Here are some possible solutions :

    • Save the model in some other way, e.g. the JSON specified here https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html
    • Limit the allowed range of xgboost versions to those that are known to work with our model. This could lead to issues in the future, for example if the aging version of xgboost we require is no longer supported by newer versions of Python.
    • Using save_model to save in JSON is worth a shot to try.