python保存模型与参数_Python深度学习实战08-保存模型与权
重
Python深度学习实战08-保存模型与权重
24 Aug 2017
本次练习将已经训练好的神经⽹络模型保存⾄本地磁盘, 如果未来仍需使⽤, 则可以直接读取调⽤. 因为训练深度学习不易, 不要不保存, 要不然以后再次需要模型, 则⼜要花好多时间成本去训练.
本次所⽤是Python深度学习实战02-Keras构建⼀个神经⽹络 · Anifacc中的神经⽹络模型, 我们可以(1)将Keras神经⽹络模型结构保存为JSON⽂件,并读取;或者将Keras神经⽹络模型结构保存为YAML⽂件,并读取;(2)将模型却总保存为HDF5格式⽂件,并读取.
让我们开始吧:
1.HDF5
我们需要安装 Python 包 h5py, 这样我们就可以将⽂件保存为 HDF5(hierarchical data format) 格式⽂件,
该格式⽂件⽤来保存⼤型实数阵列, 对于存储神经⽹络模型参数极佳. 下⾯, 我们会将⽹络权重保存为 HDF5 格式⽂件, 并在以后读取.
因为使⽤的anaconda, 所以 conda list 发现已经安装到该包, ⽆需再次安装.
2.选择⼀:JSON
将⽹络模型保存为JSON格式. del 中⾃带 to_json()(转化为json⽂件)函数, 和 model_from_json()(读取模型json⽂件函数), 同时, Keras 模型⾃带save_weights() 和 load_weights() 函数保存模型权重和加载模型权重.
代码实现如下:
# -*- coding: utf-8 -*-
# MLP for json, hdf5
dels import Sequential
from keras.layers import Dense
dels import model_from_json
import os
import numpy as np
import urllib
url = "archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
raw_data = urllib.urlopen(url)
dataset = np.loadtxt(raw_data, delimiter=",")
X = dataset[:, 0:8]
y = dataset[:, 8]
seed = 42
np.random.seed(seed)
# Create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
python解析json文件# Compile model
modelpile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, nb_epoch=150, batch_size=10, verbose=0)
# Evaluate the model
scores = model.evaluate(X, y)
print("{0}: {1:.2f}%".ics_names[1], scores[1]*100))
# Here is the Point
# save model: JSON
model_json = _json()
with open('model.json', 'w') as json_file:
c型钢规格尺寸大全json_file.write(model_json)
# save weights: HDF5
model.save_weights("model.h5")
print("Save model to disk")
# later when you want to use the model
# load json and creat model
json_file = open('model.json', 'r')
loaded_model_json = ad()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weight into new model
loaded_model.load_weights('model.h5')
print("Loaded model from disk, OK")
# Evaluate loaded model on test data
loaded_modelpile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate(X, y, verbose=0)
番禺网站建设制作print("{0}: {1:.2f}%".format(ics_names[1], scores[1]*100))
结果:
32/768 [>.............................] - ETA: 0sacc: 78.39%
Save model to disk
Loaded model from disk, OK
acc: 78.39%
2.选择⼆:YAML
除了将模型保存为JSON格式, 我们也可以保存为YAML格式, 并读取.
代码如下:
# -*- coding: utf-8 -*-
css设置宽度# MLP for json, hdf5
dels import Sequential
字典用英语怎么说dictionaryfrom keras.layers import Dense
dels import model_from_yaml
import os
import numpy as np
import urllib
url = "archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data" raw_data = urllib.urlopen(url)
dataset = np.loadtxt(raw_data, delimiter=",")
X = dataset[:, 0:8]
y = dataset[:, 8]
seed = 42
np.random.seed(seed)
# Create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
modelpile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, nb_epoch=150, batch_size=10, verbose=0)
# Evaluate the model
scores = model.evaluate(X, y)cmd5941
print("{0}: {1:.2f}%".ics_names[1], scores[1]*100))
# Here is the Point
# save model to YAML
model_yaml = _yaml()
with open('model.yaml', 'w') as yaml_file:
yaml_file.write(model_yaml)
# save weights to hdf5
model.save_weights('model1.h5')
print("Save model to disk")
#
# Load YAML to create model
yaml_file = open('model.yaml', 'r')
loaded_model_yaml = ad()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights
loaded_model.load_weights('model1.h5')
print("loaded model from disk")
# evaluate loaded model on test data
loaded_modelpile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate(X, y, verbose=0)
print("{0}: {1:.2f}%".format(ics_names[1], scores[1]*100))
结果:
Save model to disk
loaded model from disk
acc: 78.39%
Sum
到这⾥, 我们学会将训练好的模型分别保存为JSON和YAML格式⽂件, 并将权重保存为HDF5格式⽂件.申明
⽤时
代码实现: 0: 45
记录: 0: 25
总: 1:10
@Anifacc
2017-08-24
⼈⽣苦短, 为欢⼏何.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论