13.1 简介
Keras中,模型的结构和权重数据是分开的:权重的文件格式是HDF5,这种格式保存数字矩阵效率很高。模型的结构用JSON或YAML导入导出。
本章包括如何手工修改HDF5文件,使用的模型是第7章的皮马人糖尿病模型。
13.1.1 HDF5文件
分层数据格式,版本5(HDF5)可以高效保存大实数矩阵,例如神经网络的权重。HDF5的包需要安装:
sudo pip install h5py
13.2 使用JSON保存网络结构
JSON的格式很简单,Keras可以用to_json()
把模型导出为JSON格式,再用model_from_json()
加载回来。
模型和权重加载后需要编译一次,让Keras正确调用后端。模型的验证方法和之前一致:
```python
# MLP for Pima Indians Dataset serialize to JSON and HDF5
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# 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
model.compile(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, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
seed = 7
numpy.random.seed(seed)
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:,0:8]
Y = dataset[:,8]
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'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, nb_epoch=150, batch_size=10, verbose=0)
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")
结果如下。导入的模型和之前导出时一致:
acc: 79.56%
Saved model to disk
Loaded model from disk
acc: 79.56%
JSON文件类似:
"class_name": "Sequential",
"config": [{
"class_name": "Dense",
"config": {
"W_constraint": null,
"b_constraint": null,
"name": "dense_1",
"output_dim": 12,
"activity_regularizer": null,
"trainable": true,
"init": "uniform",
"input_dtype": "float32",
"input_dim": 8,
"b_regularizer": null,
"W_regularizer": null,
"activation": "relu",
"batch_input_shape": [
null,
"class_name": "Dense",
"config": {
"W_constraint": null,
"b_constraint": null,
"name": "dense_2",
"activity_regularizer": null,
"trainable": true,
"init": "uniform",
"input_dim": null,
"b_regularizer": null,
"W_regularizer": null,
"activation": "relu",
"output_dim": 8
"class_name": "Dense",
"config": {
"W_constraint": null,
"b_constraint": null,
"name": "dense_3",
"activity_regularizer": null,
"trainable": true,
"init": "uniform",
"input_dim": null,
"b_regularizer": null,
"W_regularizer": null,
"activation": "sigmoid",
"output_dim": 1
13.3 使用YAML保存网络结构
和之前JSON类似,只不过文件格式变成YAML,使用的函数变成了to_yaml()
和model_from_yaml()
:
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_yaml
import numpy
import os
seed = 7
numpy.random.seed(seed)
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:,0:8]
Y = dataset[:,8]
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'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, nb_epoch=150, batch_size=10, verbose=0)
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
model_yaml = model.to_yaml()
with open("model.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
model.save_weights("model.h5")
print("Saved model to disk")
yaml_file = open('model.yaml', 'r') loaded_model_yaml = yaml_file.read() yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate(X, Y, verbose=0)
print "%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100)
结果和之前的一样:
acc: 79.56%
Saved model to disk
Loaded model from disk
acc: 79.56%
YAML文件长这样:
class_name: Sequential
config:
- class_name: Dense
config:
W_constraint: null
W_regularizer: null
activation: relu
activity_regularizer: null
b_constraint: null
b_regularizer: null
batch_input_shape: !!python/tuple [null, 8]
init: uniform
input_dim: 8
input_dtype: float32
name: dense_1
output_dim: 12
trainable: true
- class_name: Dense
config: {W_constraint: null, W_regularizer: null, activation: relu, activity_regularizer:
null,
b_constraint: null, b_regularizer: null, init: uniform, input_dim: null, name: dense_2,
output_dim: 8, trainable: true}
- class_name: Dense
config: {W_constraint: null, W_regularizer: null, activation: sigmoid,
activity_regularizer: null,
b_constraint: null, b_regularizer: null, init: uniform, input_dim: null, name: dense_3,
output_dim: 1, trainable: true}
13.4 总结
本章关于导入导出Keras模型。总结一下:
如何用HDF5保存加载权重
如何用JSON保存加载模型
如何用YAML保存加载模型
13.4.1 下一章
模型可以保存了:下一章关于使用保存点。