简体   繁体   English

为什么我会收到持久性 sklearn 模型的 unpickling 错误?

[英]Why do I get unpickling error for persistent sklearn model?

I have a trained decision tree model which I created in a google research notebook, and pickled like this:我在谷歌研究笔记本中创建了一个训练有素的决策树模型,并像这样腌制:

pickle.dump(tree, open(tree_15.sav, 'wb'))

We have an online analysis project built from docker, creating the same environment that I used to create the model.我们有一个从 docker 构建的在线分析项目,创建的环境与我用来创建模型的环境相同。 (And some other packages for other functionality.) I want to import my model in that other project, with the following line: (以及其他一些用于其他功能的包。)我想在其他项目中导入我的模型,使用以下行:

predictive_model = pickle.load(open(self.sav_path, 'rb'), fix_imports=True, encoding='latin1')

But I get:但我得到:

_pickle.UnpicklingError: invalid load key, '\xef'.

Funny thing is, I can not reproduce the same bug locally.有趣的是,我无法在本地重现相同的错误。 I use a windows desktop (so I have problems with building from docker locally), but I use the same version of every package, the .sav file is not corrupted - hash checks out -, etc. It runs quite fine locally every time.我使用 Windows 桌面(所以我在本地从 docker 构建时遇到问题),但我使用每个包的相同版本,.sav 文件没有损坏 - 哈希检查 - 等等。它每次都在本地运行得很好。 The previous version of the same model ran online as well.同一模型的先前版本也在网上运行。 I'm quite lost here to be honest.老实说,我在这里很迷茫。

Often, gzip 's open() doesn't have the issues that the built-in open() has, so why not try the following:通常, gzipopen()没有内置open()问题,所以为什么不尝试以下操作:

import gzip, pickle
with gzip.open(self.sav_path, 'rb') as f:
    predictive_model = pickle.load(f, fix_imports=True, encoding='latin1')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试在 Flask 中加载模型时遇到 Unpickling 错误? - Why am I facing the Unpickling Error while trying to load my model in Flask? 尽管我使用相同的输入,为什么 sm.OLS 和 sklearn.linear_model 会得到不同的结果? - Why do I get different outcomes for sm.OLS and sklearn.linear_model although I use the same input? 在sklearn GridsearchCV SVM中使用class_weight时,为什么会出现错误? - Why do I get error when using class_weight in sklearn GridsearchCV SVM? Python 3:Pickling和UnPickling类实例返回“无持久加载”错误 - Python 3: Pickling and UnPickling class instances returning “no persistent load” error sklearn model 返回平均绝对误差 0,为什么? - sklearn model returns a mean absolute error of 0, why? 如果我已经将模型作为泡菜,为什么我需要在 docker 容器中使用 sklearn? - Why do I need sklearn in docker container if I already have the model as a pickle? 如何分配参数以获取每个 sklearn 回归 model 的预测目标值? - How do I assign parameter to get predicted target value per sklearn regression model? 为什么在 Django 中的 Model 上出现 KeyError? - Why do I get a KeyError on a Model in Django? 为什么会出现此错误 - Why do I get this error sklearn naive bayes MultinomialNB:尽管我有 2 个类,为什么我只得到一个带系数的数组? - sklearn naive bayes MultinomialNB: Why do I get only one array with coefficients although I have 2 classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM