深度学习入门系列8:用序列化保存模型便于继续训练

大家好,我技术人Howzit,这是深度学习入门系列第八篇,欢迎大家一起交流!

深度学习入门系列1:多层感知器概述
深度学习入门系列2:用TensorFlow构建你的第一个神经网络
深度学习入门系列3:深度学习模型的性能评价方法
深度学习入门系列4:用scikit-learn找到最好的模型
深度学习入门系列5项目实战:用深度学习识别鸢尾花种类
深度学习入门系列6项目实战:声纳回声识别
深度学习入门系列7项目实战:波士顿房屋价格回归
深度学习入门系列8:用序列化保存模型便于继续训练
深度学习入门系列9:用检查点保存训练期间最好的模型
深度学习入门系列10:从绘制记录中理解训练期间的模型行为
深度学习入门系列11:用Dropout正则减少过拟合
深度学习入门系列12:使用学习规划来提升性能
深度学习入门系列13:卷积神经网络概述
深度学习入门系列14:项目实战:基于CNN的手写数字识别
深度学习入门系列15:用图像增强改善模型性能
深度学习入门系列16:项目实战:图像中目标识别
深度学习入门系列17:项目实战:从电影评论预测情感
深度学习入门系列18:递归神经网络概述
深度学习入门系列19:基于窗口(window)的多层感知器解决时序问题
深度学习入门系列20:LSTM循环神经网络解决国际航空乘客预测问题
深度学习入门系列21:项目:用LSTM+CNN对电影评论分类
深度学习入门系列22:从猜字母游戏中理解有状态的LSTM递归神经网络
深度学习入门系列23:项目:用爱丽丝梦游仙境生成文本


鉴于深度模型的训练需要花费几个小时,几天甚至几周,了解如何保存和从磁盘加载神经网络数据是很重要的。在这节课,你将学到如何保存你的Keras模型到文件并在此加载用于预测。在完成这节课后你将了解:
  • 如何保存和加载Keras模型权重到HDF5格式文件。
  • 如何保存和加载Keras模型结构到JSON文件。
  • 如何保存和加载Keras模型结构到YAML文件。

让我们开始吧

8.1 课程概况

Keras把模型结构和权重分开保存。模型权重被保存为HDF5 格式。这种网格格式对于存储多维数组比较理想。模型结构通过JSON和YAML两种不同格式描述和保存(和加载)。

每个例子将描述了保存和加载你的模权重型数据到HDF5文件。这个例子使用了在Pima印度糖尿病二进制分类数据上同样简单的神经网络。

8.1.1 HDF5 格式

分层次数据格式,缩写为HDF5,一种灵活的数据存储格式,它方便存放大量实数数组,像我们神经网络中权重一样。你需要安装支持HDF5文件格式的python库。你可以使用python包管理系统安装,如pip。

sudo pip install h5py

8.2 保存你的神经网络模型到JSON

JSON 是一种描述分层数据的简单文件格式。Keras通过to_json()函数提供了使用json格式描述任何模型的能力。它可以保存为文件并且之后通过model_from_json()函数加载,这个函数将从JSON具体参数中创建新的模型。

使用save_weights()函数直接从模型中保存权重并且之后通过load_weights()函数加载。下面是在Pima印度数据上训练和评估的简单模型。在使用它之前编译模型已加载的模型很重要。它以至于可以使用这个模型做预测,它也使用来自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

# 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))

运行这个例子,得到下面结果,它首先显示训练模型的精度,并且数据以JSON格式保存到磁盘,模型的加载以及最后加载模型和重新评估该神经网络获得同样的结果。

acc: 78.78% 
Saved model to disk 
Loaded model from disk 
acc: 78.78%

模型的JSON格式描述像下面这样:

{  
    "keras_version":"2.1.3",  
    "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"  
}

8.3 保存神经网络模型到YAML

这个例子和JSON例子非常相似,除了模型具体参数使用YAML格式。这个模型使用YAML格式来描述,保存文件到model.yaml并且之后通过model_from_yaml()函数加载成新模型。权重的处理和上面HDF5格式保存为model.h5方法一样。

# 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 

# 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")

# 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.1.3

8.4 总结

对于从研究和开发到实操移植神经网络模型来说,保存和加载模型是很重要的能力。这节课,你将学到如何序列化你的keras深度学习模型。你已经学到:

  • 如何保存模型权重到HDF5格式文件并且之后再次加载。
  • 如何保存Keras模型定义到JSON文件并且再次加载它们。
  • 如何保存Keras模型定义到YAML文件并且再次加载它们。

8.4.1 接下来

你现在知道如何在keras中序列化深度学习模型。接下来你将学习在训练期间设置深度学习模型检查点(checkpointing)的重要性并且为了预测如何加载这些检查点模型。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术人Howzit

钱不钱的无所谓,这是一种鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值