儲存並載入您的Keras深度學習模型
Keras是一個用於深度學習的簡單而強大的Python庫。 鑑於深度學習模式可能需要數小時、數天甚至數週的時間來培訓,瞭解如何儲存並將其從磁碟中載入是很重要的。 在本文中,您將發現如何將Keras模型儲存到檔案中,並再次載入它們來進行預測。 讓我們開始吧。
- 2017/03更新:添加了首先安裝h5py的說明。在每個示例中的最終列印語句中添加了缺失的括號
- 2017/03更新:更新了Keras 2.0.2,TensorFlow 1.0.1和Theano 0.9.0的示例。
圖片版權所有:art_inthecity
教程概述
Keras將儲存模型體系結構和儲存模型權重的關注點分離開來。 模型權重被儲存為 HDF5格式。這是一種網格格式,適合儲存數字的多維陣列。 可以使用兩種不同的格式來描述和儲存模型結構:JSON和YAML。 在這篇文章中,我們將會看到兩個關於儲存和載入模型檔案的例子:
- 將模型儲存到JSON。
- 將模型儲存到YAML。
每個示例還將演示如何在HDF5格式化的檔案中儲存和載入你的模型權重。 這些例子將使用同樣簡單的網路訓練,並且這些訓練被用於Pima印第安人的糖尿病二分類資料集上。這是一個包含所有數值資料的小型資料集,很容易使用。你可以下載此資料集,並將其放置在你的工作目錄中,檔名為“pima - indians - diabetes.csv”。 確認您已安裝最新版本的Keras(截至2017年3月為v1.2.2)。 注意:您可能需要先安裝h5py:
sudo pip install h5py
將你的神經網路模型儲存到JSON
JSON是一種簡單的輕量級的資料交換格式。 Keras提供了使用帶有to_json()函式的JSON格式它有描述任何模型的功能。它可以儲存到檔案中,然後通過從JSON引數建立的新模型model_from_json()函式載入。 使用save_weights()函式直接從模型中儲存權重,並使用對稱的load_weights()函式載入。 下面的例子訓練並評估了Pima印第安人資料集上的一個簡單模型。然後將該模型轉換為JSON格式並寫入本地目錄中的model.json。網路權重寫入本地目錄中的model.h5。 從儲存的檔案載入模型和權重資料,並建立一個新的模型。在使用載入的模型之前,必須先編譯它。這樣,使用該模型進行的預測可以使用Keras後端的適當而有效的計算。 該模型以相同的方式進行評估,列印相同的評估分數。
# 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 numpy.random.seed(7) # 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, kernel_initializer='uniform', activation='relu')) model.add(Dense(8, kernel_initializer='uniform', activation='relu')) model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, epochs=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') # later... # load json and create model json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights('model.h5') print('Loaded model from disk') # evaluate loaded model on test data 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: 78.78%
Saved model to disk
Loaded model from disk
acc: 78.78%
這個模型的JSON格式如下所示:
{
'keras_version':'2.0.2',
'backend':'theano',
'config':[
{
'config':{
'dtype':'float32',
'bias_regularizer':null,
'activation':'relu',
'bias_constraint':null,
'use_bias':true,
'bias_initializer':{
'config':{
},
'class_name':'Zeros'
},
'kernel_regularizer':null,
'activity_regularizer':null,
'kernel_constraint':null,
'trainable':true,
'name':'dense_1',
'kernel_initializer':{
'config':{
'maxval':0.05,
'minval':-0.05,
'seed':null
},
'class_name':'RandomUniform'
},
'batch_input_shape':[
null,
8
],
'units':12
},
'class_name':'Dense'
},
{
'config':{
'kernel_regularizer':null,
'bias_regularizer':null,
'activation':'relu',
'bias_constraint':null,
'use_bias':true,
'bias_initializer':{
'config':{
},
'class_name':'Zeros'
},
'activity_regularizer':null,
'kernel_constraint':null,
'trainable':true,
'name':'dense_2',
'kernel_initializer':{
'config':{
'maxval':0.05,
'minval':-0.05,
'seed':null
},
'class_name':'RandomUniform'
},
'units':8
},
'class_name':'Dense'
},
{
'config':{
'kernel_regularizer':null,
'bias_regularizer':null,
'activation':'sigmoid',
'bias_constraint':null,
'use_bias':true,
'bias_initializer':{
'config':{
},
'class_name':'Zeros'
},
'activity_regularizer':null,
'kernel_constraint':null,
'trainable':true,
'name':'dense_3',
'kernel_initializer':{
'config':{
'maxval':0.05,
'minval':-0.05,
'seed':null
},
'class_name':'RandomUniform'
},
'units':1
},
'class_name':'Dense'
}
],
'class_name':'Sequential'
}
將你的神經網路模型儲存到YAML
此示例與上述JSON示例大致相同,但YAML格式用於模型規範。 該模型使用YAML進行描述,儲存到檔案model.yaml。yaml和later通過model_from_yaml()函式載入到新模型中。權重的處理方式同樣以HDF5格式儲存在model.5
# MLP for Pima Indians Dataset serialize to YAML and HDF5
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_yaml
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, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=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 YAML
model_yaml = model.to_yaml()
with open('model.yaml', 'w') as yaml_file:
yaml_file.write(model_yaml)
# serialize weights to HDF5
model.save_weights('model.h5')
print('Saved model to disk')
# later...
# load YAML and create model
yaml_file = open('model.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
loaded_model.load_weights('model.h5')
print('Loaded model from disk')
# evaluate loaded model on test data
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: 78.78%
Saved model to disk
Loaded model from disk
acc: 78.78%
YAML格式描述的模型如下:
backend: theano
class_name: Sequential
config:
- class_name: Dense
config:
activation: relu
activity_regularizer: null
batch_input_shape: !!python/tuple [null, 8]
bias_constraint: null
bias_initializer:
class_name: Zeros
config: {}
bias_regularizer: null
dtype: float32
kernel_constraint: null
kernel_initializer:
class_name: RandomUniform
config: {maxval: 0.05, minval: -0.05, seed: null}
kernel_regularizer: null
name: dense_1
trainable: true
units: 12
use_bias: true
- class_name: Dense
config:
activation: relu
activity_regularizer: null
bias_constraint: null
bias_initializer:
class_name: Zeros
config: {}
bias_regularizer: null
kernel_constraint: null
kernel_initializer:
class_name: RandomUniform
config: {maxval: 0.05, minval: -0.05, seed: null}
kernel_regularizer: null
name: dense_2
trainable: true
units: 8
use_bias: true
- class_name: Dense
config:
activation: sigmoid
activity_regularizer: null
bias_constraint: null
bias_initializer:
class_name: Zeros
config: {}
bias_regularizer: null
kernel_constraint: null
kernel_initializer:
class_name: RandomUniform
config: {maxval: 0.05, minval: -0.05, seed: null}
kernel_regularizer: null
name: dense_3
trainable: true
units: 1
use_bias: true
keras_version: 2.0.2
總結
在這篇文章中,你發現瞭如何序列化你的Keras深度學習模型。 你瞭解瞭如何將訓練的模型儲存到檔案中,然後將它們載入並使用它們進行預測。 你還了解到,模型權重很容易使用HDF5格式儲存,而網路結構可以以JSON或YAML格式儲存。