Xgboost 转储和加载问题

Pet*_*etr 11 python pickle xgboost

我在 amazon sagemaker 上训练了我的 xgboost 管道模型并在本地保存文件:

pickle.dump(model, open(file_name, "wb"))
Run Code Online (Sandbox Code Playgroud)

然后移动到本地计算机以推理模式使用模型:

pickle.load(open(file_name, "rb"))

XGBoostError: [11:45:49] ../src/common/json.cc:458: Expecting: """, got: "76 ", around character position: 1
    {L\0\0\0\0\0\0\0
    ^~~~~~~~~
Stack trace:
  [bt] (0) 1   libxgboost.dylib                    0x00000001258a9a54 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   libxgboost.dylib                    0x00000001258d69be xgboost::JsonReader::Error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) const + 1054
  [bt] (2) 3   libxgboost.dylib                    0x00000001258d7427 xgboost::JsonReader::Expect(char, char) + 279
  [bt] (3) 4   libxgboost.dylib                    0x00000001258d7bfb xgboost::JsonReader::ParseObject() + 331
  [bt] (4) 5   libxgboost.dylib                    0x00000001258d6467 xgboost::JsonReader::Parse() + 439
  [bt] (5) 6   libxgboost.dylib                    0x00000001258dbfbe xgboost::Json::Load(xgboost::StringView) + 62
  [bt] (6) 7   libxgboost.dylib                    0x0000000125960b93 xgboost::LearnerIO::Load(dmlc::Stream*) + 435
  [bt] (7) 8   libxgboost.dylib                    0x00000001258b510f XGBoosterUnserializeFromBuffer + 159
  [bt] (8) 9   libffi.7.dylib                      0x000000010f72bead ffi_call_unix64 + 85

Run Code Online (Sandbox Code Playgroud)

sagemaker 上使用的软件包版本:xgboost==1.6.1

Ser*_*aph 2

将xgboost更新到1.7.1即可解决问题。

当您加载在较新版本的 XGBoost 包中生成的模型时,通常会发生这种情况。原因是新版本在生成树时将“True”和“False”替换为 1 和 0。当旧版本的 XGBoost 尝试加载模型时,它不理解 1 或 0 代表什么。