第13章 用序列化保存模型

深度学习的模型有可能需要好几天才能训练好,如果没有SL大法就完蛋了。本章关于如何保存和加载模型。本章将:

  • 使用HDF5格式保存模型
  • 使用JSON格式保存模型
  • 使用YAML格式保存模型

我们开始吧。

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

导入:

# later...
# load json and create model
# 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")
# later...
# load json and create model

结果如下。导入的模型和之前导出时一致:

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,8]}},{"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()

# 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, 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 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: 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章 用序列化保存模型相关推荐

  1. 第13章 二叉树模型及其扩展

    这学期会时不时更新一下伊曼纽尔·德曼(Emanuel Derman) 教授与迈克尔B.米勒(Michael B. Miller)的<The Volatility Smile>这本书,本意是 ...

  2. 第 13 章 StringTable

    第 13 章 StringTable 1.String 的基本特性 1.1.String 概述 String 的概述 String:字符串,使用一对 "" 引起来表示 String ...

  3. 《利用Python进行数据分析·第2版》第13章 Python建模库介绍

    第1章 准备工作 第2章 Python语法基础,IPython和Jupyter 第3章 Python的数据结构.函数和文件 第4章 NumPy基础:数组和矢量计算 第5章 pandas入门 第6章 数 ...

  4. servlet3.1规范翻译:第13章 安全

    servlet3.1规范翻译:第13章 安全 2013-02-21 16:55 1563人阅读 评论(0) 收藏 举报  分类: servlet3.1规范翻译(17)  目录(?)[+] 第13章 安 ...

  5. 第六章 使用scikit-learn构建模型

    第六章 使用scikit-learn构建模型 任务6.1 使用sklearn转换器处理数据 6.1.1 加载datasets模块中的数据集 #加载breast_canser数据集 from sklea ...

  6. Linux就这个范儿 第13章 打通任督二脉

    Linux就这个范儿 第13章 打通任督二脉 0111010110--你有没有想过,数据从看得见或看不见的线缆上飞来飞去,是怎么实现的呢?数据传输业务的未来又在哪里?在前面两章中我们学习了Linux网 ...

  7. python建立分析模型_《利用Python进行数据分析》13.2 使用Patsy创建模型描述

    第十三章 Python建模库介绍 13.2 使用Patsy创建模型描述 Patsy(https://patsy.readthedocs.io/)是一个用于描述统计模型(尤其是线性模型)的Python库 ...

  8. 第13章 面向对象编程

    第13章 面向对象编程 JavaScript是基于原型继承的机制来实现面向对象编程的.例如,对象直接量继承于Object.prototype,函数对象继承于Function.prototype,而pr ...

  9. UML参考手册 第三部分  参 考 资 料 第13章 术 语 大 全 九 (转)

    UML参考手册 第三部分 参 考 资 料 第13章 术 语 大 全 (转)[@more@]UML参考手册     第三部分 参 考 资 料 第13章 术 语 大 全 335.概要(summarizat ...

  10. 《机器学习》慕课版课后习题-第13章

    中国工信出版集团.人民邮电出版社出版的赵卫东.董亮编著的<机器学习>慕课版 第13章 推荐系统 1.推荐系统的功能是什么? 解:推荐系统是一种帮助用户快速发现有用信息的工具.通过分析用户的 ...

最新文章

  1. Cpp / Hash 所得字符串转成 Hex 字符串。
  2. redis集群扩容和缩容_深入理解Redis Cluster集群
  3. Erlang TCP Socket的接收进程的2种方案
  4. 【qduoj - 夏季学期创新题】C语言课程设计-阶梯问题(dp,高精度大数)
  5. mysql 分区表_MySQL 分区分表应用场景分析和分区中可能遇到的坑点
  6. HTML5 footer元素
  7. 奖励名单表格模板_“我用一套表格,解决了孩子的拖延症,一路用到小学高年级!”...
  8. c语言十进制小数转其他进制,只写出了十进制小数转换成二进制的,求二进制小数转十进制的...
  9. CSS3---渲染属性
  10. 视频教程-Excel VBA网抓教程【你学得会】-Office/WPS
  11. ubuntu安装公式编辑器mathtype, wine中文乱码,ubuntu中文字体
  12. 用python证明采样定理_这一切都从指数函数开始(4)——采样定理
  13. 大数据舆情监测平台_大数据舆情监测与分析平台有哪些?舆情大数据监测软件排名2020...
  14. weex请求方法stream 的封装
  15. 【JAVAEE框架】浅谈 Spring 框架的两大核心思想 AOP 与 IOP
  16. 问题 H: A+B 输入输出练习VIII
  17. USACO-Arithmetic Progressions
  18. C是C++的子集吗?
  19. GOF设计模式之单例模式
  20. 计算机数学ppt,数学主题ppt

热门文章

  1. 递归神经网络/_递归神经网络
  2. 前端js使用java变量值_web前端:js中的变量
  3. 随手记--Windows系统下的cmd和powershell的区别
  4. mysql mysqlhotcopy_mysql中mysqlhotcopy备份数据库总结
  5. 51Nod 1593 公园晨跑(RMQ,ST表)
  6. docker 容器无法连接外网
  7. Postgresql pg_dumppg_restore用法
  8. 移动端 重定向 https 请求
  9. PostgreSQL 按周、月、天 统计问题
  10. linux 多路径配置