You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载XGboost模型pickle文件遇Json解析异常,求解决方案

XGBoost加载Pickle模型时Json解析错误的解决方法

我使用XGBoost库执行预测任务,加载pickle格式的模型文件时出现如下错误:

File "/Users/admin/space/ML/model_building/predictor.py", line 74, in load_regressor
    self._regressor = pickle.load(f)
  File "/Users/admin/.conda/envs/env/lib/python3.8/site-packages/xgboost/core.py", line 1496, in __setstate__
    _check_call(
  File "/Users/admin/.conda/envs/env/lib/python3.8/site-packages/xgboost/core.py", line 203, in _check_call
    raise XGBoostError(py_str(_LIB.XGBGetLastError()))
xgboost.core.XGBoostError: [16:23:50] /Users/runner/miniforge3/conda-bld/xgboost-split_1631904775127/work/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                    0x000000014c7dce74 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   libxgboost.dylib                    0x000000014c80e5ce xgboost::JsonReader::Error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) const + 1070
  [bt] (2) 3   libxgboost.dylib                    0x000000014c80f09b xgboost::JsonReader::Expect(char, char) + 299
  [bt] (3) 4   libxgboost.dylib                    0x000000014c80f93b xgboost::JsonReader::ParseObject() + 491
  [bt] (4) 5   libxgboost.dylib                    0x000000014c80e068 xgboost::JsonReader::Parse() + 456
  [bt] (5) 6   libxgboost.dylib                    0x000000014c813fce xgboost::Json::Load(xgboost::StringView) + 62
  [bt] (6) 7   libxgboost.dylib                    0x000000014c893a2f xgboost::LearnerIO::Load(dmlc::Stream*) + 607
  [bt] (7) 8   libxgboost.dylib                    0x000000014c7e822f XGBoosterUnserializeFromBuffer + 159
  [bt] (8) 9   libffi.8.dylib                      0x000000010e368d92 ffi_call_unix64 + 82 

该pickle模型文件在其他机器上可正常加载,但本地机器使用相同版本的XGBoost(1.6.1)却出现此错误,以下是可行的解决方法:

解决方法

  • 校验模型文件完整性:对比本地模型文件与其他正常机器上文件的MD5哈希值,确认文件在传输或存储过程中未损坏。若不一致,重新获取完整的模型文件。
  • 使用XGBoost原生序列化方式:放弃pickle,改用XGBoost官方推荐的save_model和load_model方法,这能避免跨环境的pickle兼容性问题:
    # 保存模型(在原机器执行)
    model.save_model("xgb_regressor.model")
    
    # 加载模型(本地机器执行)
    from xgboost import XGBRegressor
    model = XGBRegressor()
    model.load_model("xgb_regressor.model")
    
  • 同步环境依赖版本:即使XGBoost版本相同,numpy、scipy等依赖库的版本差异也可能导致问题。导出其他正常机器的依赖清单,在本地环境中安装完全一致的版本:
    # 导出依赖清单(正常机器执行)
    conda env export > environment.yml
    
    # 本地创建相同环境
    conda env create -f environment.yml
    
  • 针对Mac M系列芯片重新编译XGBoost:若本地是苹果M系列芯片,预编译的XGBoost包可能存在架构兼容性问题,尝试从源码编译安装:
    git clone --recursive https://github.com/dmlc/xgboost
    cd xgboost
    mkdir build && cd build
    cmake ..
    make -j$(sysctl -n hw.ncpu)
    cd ../python-package
    python setup.py install
    

内容的提问来源于stack exchange,提问作者Hami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 09:10:44