【问题标题】:NotFoundError while using pickle dump to save a model使用 pickle 转储保存模型时出现 NotFoundError
【发布时间】:2022-01-10 01:18:59
【问题描述】:

我创建了一个名为“model”的模型,但是当我尝试使用 pickle 保存它时,它只会给出一个“NotFoundError”。

import pickle
with open("test.pkl","wb") as file:
    pickle.dump(model, file)

这是我在运行代码时收到的错误消息。

错误信息

INFO:tensorflow:Assets written to: ram://471dfd58-f3fe-4d9f-9075-60a1568cc629/assets
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
<ipython-input-47-0abd122520e5> in <module>
      1 import pickle
      2 with open("test.pkl","wb") as file:
----> 3     pickle.dump(model, file)

~\anaconda3\lib\site-packages\keras\engine\training.py in __reduce__(self)
    313     if self.built:
    314       return (pickle_utils.deserialize_model_from_bytecode,
--> 315               pickle_utils.serialize_model_as_bytecode(self))
    316     else:
    317       # SavedModel (and hence serialize_model_as_bytecode) only support

~\anaconda3\lib\site-packages\keras\saving\pickle_utils.py in serialize_model_as_bytecode(model)
     75         with tf.io.gfile.GFile(dest_path, "rb") as f:
     76           info = tarfile.TarInfo(name=os.path.relpath(dest_path, temp_dir))
---> 77           info.size = f.size()
     78           archive.addfile(tarinfo=info, fileobj=f)
     79   tf.io.gfile.rmtree(temp_dir)

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in size(self)
     97   def size(self):
     98     """Returns the size of the file."""
---> 99     return stat(self.__name).length
    100 
    101   def write(self, file_content):

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in stat(filename)
    908     errors.OpError: If the operation fails.
    909   """
--> 910   return stat_v2(filename)
    911 
    912 

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in stat_v2(path)
    924     errors.OpError: If the operation fails.
    925   """
--> 926   return _pywrap_file_io.Stat(compat.path_to_str(path))
    927 
    928 

NotFoundError: 

任何帮助将不胜感激

【问题讨论】:

  • 您的错误显示读取文件 open('model_pkl' , 'rb')pickle.load(f) 有问题,而不是写入问题。
  • @furas 实际上 open('model_pkl','rb') 是此错误消息之后的下一个单元格,它被错误地复制了。错误消息以“NotFoundError:”结尾。我会进行编辑。

标签: python tensorflow machine-learning scikit-learn pickle


【解决方案1】:

不是一个解决方案,但希望它仍然有用。

我遇到了同样的问题,同样的错误。我最终通过使用 Keras 保存和加载方法而不是 pickle 来避免它。我不知道您的型号是什么,但您可能想尝试相同的型号。这可能是由于pickeleable 的原因。也许给this 的答案会有所帮助,他们主张单独保存 tensorflow 对象。

【讨论】:

  • 感谢您的评论。我知道我来得太晚了。我最终做了同样的事情,使用了 keras save 并将我的模型保存为 .h5 配置,它可以正常工作。
猜你喜欢
  • 2021-03-17
  • 2020-12-18
  • 2020-06-14
  • 2019-07-19
  • 2018-08-03
  • 2011-06-02
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多