XGBClassifer,反序列化时,给出 'XGBModel' object has no attribute 'enable_categorical'
XGBClassifer, when de-serialized, gives 'XGBModel' object has no attribute 'enable_categorical'
我有一个序列化的 XGBClassifier 对象,使用 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)
我使用以下方法加载对象:
clf_model = joblib.load(model_path)
我想使用该对象来预测我正在使用也具有 xgboost=1.5.2 的 Azure 环境的某些数据。但它给出了错误:
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'
我们在管道中具有与 produce/serialize 模型相同的版本,并且在反序列化模型以预测新数据的管道中具有相同的版本。
以下是一些可能的解决方案:
- 以其他方式保存模型,例如这里指定的 JSON https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html
- 将 xgboost 版本的允许范围限制为已知可与我们的模型一起使用的版本。这可能会导致将来出现问题,例如,如果我们需要的 xgboost 的旧版本不再受 Python.
的较新版本支持
- 使用
save_model
保存 JSON 值得一试。
我有一个序列化的 XGBClassifier 对象,使用 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)
我使用以下方法加载对象:
clf_model = joblib.load(model_path)
我想使用该对象来预测我正在使用也具有 xgboost=1.5.2 的 Azure 环境的某些数据。但它给出了错误:
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'
我们在管道中具有与 produce/serialize 模型相同的版本,并且在反序列化模型以预测新数据的管道中具有相同的版本。
以下是一些可能的解决方案:
- 以其他方式保存模型,例如这里指定的 JSON https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html
- 将 xgboost 版本的允许范围限制为已知可与我们的模型一起使用的版本。这可能会导致将来出现问题,例如,如果我们需要的 xgboost 的旧版本不再受 Python. 的较新版本支持
- 使用
save_model
保存 JSON 值得一试。