簡體   English   中英

無法打開腌制的 Sagemaker XGBoost 模型

[英]Unable to open pickled Sagemaker XGBoost model

我正在嘗試打開我在 AWS Sagemaker 中創建的腌制 XGBoost 模型,以查看模型中的特征重要性。 我正在嘗試按照這篇文章中的答案進行操作。 但是,我收到如下所示的錯誤。 當我嘗試調用Booster.save_model ,我收到一條錯誤消息,說'Estimator' object has no attribute 'save_model' 我該如何解決這個問題?

# Build initial model
sess = sagemaker.Session()
s3_input_train = sagemaker.s3_input(s3_data='s3://{}/{}/train/'.format(bucket, prefix), content_type='csv')
xgb_cont = get_image_uri(region, 'xgboost', repo_version='0.90-1')
xgb = sagemaker.estimator.Estimator(xgb_cont, role, train_instance_count=1, train_instance_type='ml.m4.4xlarge',
                                    output_path='s3://{}/{}'.format(bucket, prefix), sagemaker_session=sess)
xgb.set_hyperparameters(eval_metric='rmse', objective='reg:squarederror', num_round=100)
ts = strftime("%Y-%m-%d-%H-%M-%S", gmtime())
xgb_name = 'xgb-initial-' + ts
xgb.set_hyperparameters(eta=0.1, alpha=0.5, max_depth=10)
xgb.fit({'train': s3_input_train}, job_name=xgb_name)

# Load model to get feature importances
model_path = 's3://{}/{}//output/model.tar.gz'.format(bucket, prefix, xgb_name)
fs = s3fs.S3FileSystem()
with fs.open(model_path, 'rb') as f:
    with tarfile.open(fileobj=f, mode='r') as tar_f:
        with tar_f.extractfile('xgboost-model') as extracted_f:
            model = pickle.load(extracted_f)

XGBoostError: [19:16:42] /workspace/src/learner.cc:682: Check failed: header == serialisation_header_: 

  If you are loading a serialized model (like pickle in Python) generated by older
  XGBoost, please export the model by calling `Booster.save_model` from that version
  first, then load it back in current version.  There's a simple script for helping
  the process. See:

    https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html

  for reference to the script, and more details about differences between saving model and
  serializing.

您在筆記本中使用的是哪個版本的 XGBoost? XGBoost 1.0 中的模型格式已更改。 請參閱https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html 簡短版本:如果您在筆記本中使用 1.0,則無法加載腌制模型。

這是一個在腳本模式下使用 XGBoost 的工作示例(比內置算法靈活得多):

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM