【问题标题】:Can't save trained Neural Network using keras无法使用 keras 保存训练有素的神经网络
【发布时间】:2017-06-22 15:19:22
【问题描述】:

我无法以 h5py 格式保存我训练的神经网络。它显示以下错误:

ImportError                               Traceback (most recent call last)
<ipython-input-64-0185b568b480> in <module>()
      1 from keras.models import load_model
----> 2 model.save("MNISTclassifier.h5")

/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer)
   2470         """
   2471         from ..models import save_model
-> 2472         save_model(self, filepath, overwrite, include_optimizer)
   2473 
   2474     def save_weights(self, filepath, overwrite=True):

/usr/local/lib/python3.5/dist-packages/keras/models.py in save_model(model, filepath, overwrite, include_optimizer)
     53 
     54     if h5py is None:
---> 55         raise ImportError('`save_model` requires h5py.')
     56 
     57     def get_json_type(obj):

ImportError: `save_model` requires h5py.

即使我已经使用 pip 安装了 h5py 并导入了它。 源代码在这里: https://github.com/tanmay-edgelord/HandwrittenDigitRecognition/blob/master/MNIST%20.ipynb

【问题讨论】:

  • 建议的修复here 会解决您的问题吗?
  • 重启电脑解决了我的问题。我什至没有更改我的代码。就像我什至没有添加评论一样。

标签: python-3.x keras h5py


【解决方案1】:

您可以使用model.save(filepath) 将 Keras 模型保存到单个 HDF5 文件中,该文件将包含:

  1. 模型的架构,允许重新创建模型
  2. 模型的权重
  3. 训练配置(损失、优化器)
  4. 优化器的状态,允许在您停止的地方恢复训练。

然后您可以使用keras.models.load_model(filepath) 重新实例化您的模型。 load_model 还将负责使用保存的训练配置编译模型(除非模型一开始就从未编译过)。

例子:

from keras.models import load_model
model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2019-07-23
    • 2017-02-20
    • 2011-04-07
    相关资源
    最近更新 更多