如何在Linux系统中加载pickle模型

时间:2020-01-10 10:48:29

标签: python serialization amazon-ec2 devops xgboost

我已经在python中使用xgboost软件包构建了Xgb模型。我使用pickle和joblib保存了模型,该模型在我的Windows 10系统中完美运行。但这在我尝试运行的AWS实例中给出了错误。该错误似乎与序列化有关。

>>> import joblib
>>> joblib.load(xgb_low_lr_fin.sav)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xgb_low_lr_fin' is not defined
>>> joblib.load("xgb_low_lr_fin.sav")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 598, in load
    obj = _unpickle(fobj, filename, mmap_mode)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 526, in _unpickle
    obj = unpickler.load()
  File "/home/ubuntu/anaconda3/lib/python3.7/pickle.py", line 1085, in load
    dispatch[key[0]](self)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 339, in load_build
    Unpickler.load_build(self)
  File "/home/ubuntu/anaconda3/lib/python3.7/pickle.py", line 1549, in load_build
    setstate(state)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/xgboost-1.0.0_SNAPSHOT-py3.7.egg/xgboost/core.py", line 1132, in __setstate__
    _LIB.XGBoosterUnserializeFromBuffer(handle, ptr, length))
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/xgboost-1.0.0_SNAPSHOT-py3.7.egg/xgboost/core.py", line 189, in _check_call
    raise XGBoostError(py_str(_LIB.XGBGetLastError()))
xgboost.core.XGBoostError: [10:43:02] src/learner.cc:660: Check failed: header == serialisation_header_ (

以上代码在Windows和Mac OS中均能完美运行。

2 个答案:

答案 0 :(得分:0)

当尝试加载在另一台机器上腌制的腌制文件时,我遇到了类似的问题,这与在2和3甚至在腌制本身的不同版本中腌制序列化不同有关。

尝试检查您的pickle和python版本,并使其匹配:)

答案 1 :(得分:0)

在我的情况下,此错误是由我的xgboost版本略有不同引起的。我的模型是使用xgboost的CPU实现进行训练的。在构建并安装具有GPU支持的xgboost之后,不再加载在CPU上训练的模型。从pypi重新安装普通版本的xgboost似乎可以解决我的问题。