我如何保存ternsorflow keras模型?

问题描述 投票:1回答:1

我正在尝试使用此摘要保存张量流keras模型:

Model: "sequential_2" etc.

使用以下命令:

model.save('my_model.h5')

我在Jupyter Notebook中收到以下错误:

ImportError: `save_model` requires h5py.

所以,我安装了h5py,使用conda install h5py安装完成后,我在Jupyter Notebook中获得了h5py的版本(我正在尝试保存模型的相同位置):

h5py.__version__
'2.8.0'

仍然,我遇到了同样的错误。即使我手动导入了h5py。

import h5py
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
filename = 'model.h5' 
model.save(filename)

---------------------------------------------------------------------------
**ImportError**                               Traceback (most recent call last)
<ipython-input-54-9160eee81fe6> in <module>
      5 from tensorflow.keras import layers
      6 filename = 'model.h5'
----> 7 model.save(filename)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in save_model_to_hdf5(model, filepath, overwrite, include_optimizer)

**ImportError:** save_model requires h5py.
python tensorflow keras save h5py
1个回答
0
投票

您是否尝试过直接安装h5py?如果您已有Python安装(例如python.org下载或操作系统随附的安装),则在Windows,macOS / OSX和Intel计算机上的Linux上,可以通过以下方式通过pip安装预构建的h5py轮子: PyPI:

$ pip install h5py

Windows中:如果失败,您可以尝试安装cythonpip install h5pypip install cython

Linux / Ubuntu中:如果失败,那么您可能还需要libhdf5安装libhdf5,然后安装h5py1.sudo pip install cython2.sudo apt-get install libhdf5-dev3. sudo pip install h5py

© www.soinside.com 2019 - 2024. All rights reserved.