简体   繁体   中英

How do I save a ternsorflow keras model?

I am trying to save a tensorflow keras model with this summary:

Model: "sequential_2" etc.

using the following command:

model.save('my_model.h5')

I got the following error (in Jupyter Notebook):

ImportError: `save_model` requires h5py.

So, I installed h5py, using conda install h5py After the installation, I got the version of h5py in the Jupyter Notebook(same place I am trying to save the model):

h5py.__version__
'2.8.0'

Still, I got the same error. Even though, I imported h5py manually.

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.

Have you tried directly installing h5py ?
If you have an existing Python installation (eg a python.org download, or the one that comes with your OS), then on Windows, macOS/OSX, and Linux on Intel computers, pre-built h5py wheels can be installed via pip from PyPI:

$ pip install h5py

In Windows :
If that fails you can try installing cython
pip install h5py
pip install cython

In Linux/Ubuntu :
If that fails then you may also need libhdf5 installing libhdf5 and then installing h5py
1. sudo pip install cython
2. sudo apt-get install libhdf5-dev
3. sudo pip install h5py

Note: If the above installation didn't work try uninstalling h5py and then reinstall it and restart the jupyter notebook or anaconda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM