Ubuntu中利用h5py保存训练好的keras神经网络模型

Ubuntu中利用h5py保存训练好的keras神经网络模型
利用h5py保存的模型所占的空间非常小。在利用h5py保存keras训练好的模型之前需要先安装h5py,具体安装过程詳細如下。


(1)利用h5py保存和读取keras模型的代码如下:

import h5py from keras.models import model_from_json
json_string = model.to_json()
open('my_model_architecture.json','w').write(json_string)
model.save_weights('my_model_weights.h5')
 #读取model
model = model_from_json(open('my_model_architecture.json').read())
model.load_weights('my_model_weights.h5')

(2)安装h5py步驟:

直接利用 sudo pip install h5py 首先出现没有cython;安装完cython后会提示一个g++错误,这是由于没有安装hdf5;安装完hdf5再安装h5py就能够成功安装。

安装h5py的命令如下:

sudo pip install cython
sudo apt-get install libhdf5-dev
sudo pip install h5py

安装完成后可以用如下命令測試:

userT@t5810:~$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import h5py
>>> 

轉載自:

http://blog.csdn.net/linmingan/article/details/50736300

http://blog.csdn.net/linmingan/article/details/50736615



猜你喜欢

转载自blog.csdn.net/jdbc/article/details/72736793