一、sklearn模型保存与读取 
1、保存

 from sklearn.externals import joblib
from sklearn import svm
X = [[0, 0], [1, 1]]
y = [0, 1]
clf = svm.SVC()
clf.fit(X, y)
joblib.dump(clf, "train_model.m")

2、读取

 clf = joblib.load("train_model.m")
clf.predit([0,0]) #此处test_X为特征集

二、TensorFlow模型保存与读取(该方式tensorflow只能保存变量而不是保存整个网络,所以在提取模型时,我们还需要重新第一网络结构。) 
1、保存

 import tensorflow as tf
import numpy as np W = tf.Variable([[1,1,1],[2,2,2]],dtype = tf.float32,name='w')
b = tf.Variable([[0,1,2]],dtype = tf.float32,name='b') init = tf.initialize_all_variables()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
save_path = saver.save(sess,"save/model.ckpt")

2、加载

 import tensorflow as tf
import numpy as np W = tf.Variable(tf.truncated_normal(shape=(2,3)),dtype = tf.float32,name='w')
b = tf.Variable(tf.truncated_normal(shape=(1,3)),dtype = tf.float32,name='b') saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"save/model.ckpt")

三、TensorFlow模型保存与读取(该方式tensorflow保存整个网络) 
1、保存

 import tensorflow as tf

 # First, you design your mathematical operations
# We are the default graph scope # Let's design a variable
v1 = tf.Variable(1. , name="v1")
v2 = tf.Variable(2. , name="v2")
# Let's design an operation
a = tf.add(v1, v2) # Let's create a Saver object
# By default, the Saver handles every Variables related to the default graph
all_saver = tf.train.Saver()
# But you can precise which vars you want to save under which name
v2_saver = tf.train.Saver({"v2": v2}) # By default the Session handles the default graph and all its included variables
with tf.Session() as sess:
# Init v and v2
sess.run(tf.global_variables_initializer())
# Now v1 holds the value 1.0 and v2 holds the value 2.0
# We can now save all those values
all_saver.save(sess, 'data.chkp')
# or saves only v2
v2_saver.save(sess, 'data-v2.chkp')
模型的权重是保存在 .chkp 文件中,模型的图是保存在 .chkp.meta 文件中。

2、加载

 import tensorflow as tf

 # Let's laod a previous meta graph in the current graph in use: usually the default graph
# This actions returns a Saver
saver = tf.train.import_meta_graph('results/model.ckpt-1000.meta') # We can now access the default graph where all our metadata has been loaded
graph = tf.get_default_graph() # Finally we can retrieve tensors, operations, etc.
global_step_tensor = graph.get_tensor_by_name('loss/global_step:0')
train_op = graph.get_operation_by_name('loss/train_op')
hyperparameters = tf.get_collection('hyperparameters') 恢复权重 请记住,在实际的环境中,真实的权重只能存在于一个会话中。也就是说,restore 这个操作必须在一个会话中启动,然后将数据权重导入到图中。理解恢复操作的最好方法是将它简单的看做是一种数据初始化操作。
with tf.Session() as sess:
# To initialize values with saved data
saver.restore(sess, 'results/model.ckpt-1000-00000-of-00001')
print(sess.run(global_step_tensor)) # returns 1000

四、keras模型保存和加载

 model.save('my_model.h5')
model = load_model('my_model.h5')

Sklearn,TensorFlow,keras模型保存与读取的更多相关文章

  1. keras模型保存和权重保存

    模型保存和读取(包括权重): model.save('./model.h5') from keras import models model = models.load_model(./model.h ...

  2. [MISS静IOS开发原创文摘]-AppDelegate存储全局变量和 NSUserDefaults standardUserDefaults 通过模型保存和读取数据,存储自定义的对象

    由于app开发的需求,需要从api接口获得json格式数据并保存临时的 app的主题颜色 和 相关url 方案有很多种: 1, 通过AppDelegate保存为全局变量,再获取 2,使用NSUSerD ...

  3. 10 Tensorflow模型保存与读取

    我们的模型训练出来想给别人用,或者是我今天训练不完,明天想接着训练,怎么办?这就需要模型的保存与读取.看代码: import tensorflow as tf import numpy as np i ...

  4. 『TensorFlow』模型保存和载入方法汇总

    『TensorFlow』第七弹_保存&载入会话_霸王回马 一.TensorFlow常规模型加载方法 保存模型 tf.train.Saver()类,.save(sess, ckpt文件目录)方法 ...

  5. (六) Keras 模型保存和RNN简单应用

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 RNN用于图 ...

  6. TensorFlow的模型保存与加载

    import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf #tensorboard --logdir=&qu ...

  7. Pytorch model saving and loading 模型保存和读取

    It is really useful to save and reload the model and its parameters during or after training in deep ...

  8. keras中的模型保存和加载

    tensorflow中的模型常常是protobuf格式,这种格式既可以是二进制也可以是文本.keras模型保存和加载与tensorflow不同,keras中的模型保存和加载往往是保存成hdf5格式. ...

  9. Python之TensorFlow的模型训练保存与加载-3

    一.TensorFlow的模型保存和加载,使我们在训练和使用时的一种常用方式.我们把训练好的模型通过二次加载训练,或者独立加载模型训练.这基本上都是比较常用的方式. 二.模型的保存与加载类型有2种 1 ...

随机推荐

  1. Mosquitto 单向SSL配置

    Mosquitto 单向SSL配置 摘自:https://blog.csdn.net/a_bcd_123/article/details/70167833 2017年04月14日 06:56:06 s ...

  2. Linux 设置默认编辑器(以nano为例)

    查看nano地址 which nano output: /usr/bin/nano 设置默认编辑器 nano ~/.bashrc export EDITOR=nano alias vi=/usr/bi ...

  3. Qt编译,imp_CommandLineToArgvW@8问题

    Tested msvc2013. The linker can not find _imp_CommandLineToArgvW@8. It's in shell32.lib. I see qtmai ...

  4. mybatis-generator命令行生成代码

    目录文件如下: generator.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <!DOC ...

  5. Java Decompiler(Java反编译工具)

    参考:http://blog.csdn.net/yulei_qq/article/details/24175547 Java Decompiler可以对整个jar包进行反编译,也可以将其集成到ecli ...

  6. 编写高质量代码改善C#程序的157个建议——建议121:为应用程序设定运行权限

    建议121:为应用程序设定运行权限 在某些情况下,可能存在这样的需求:只有系统管理员才能访问某应用程序的若干功能.这个时候,可以结合.NET中提供的代码访问安全性(Code Access Securi ...

  7. 【小梅哥SOPC学习笔记】NIOS II处理器运行UC/OS II

    SOPC开发流程之NIOS II 处理器运行 UC/OS II 这里以在芯航线FPGA学习套件的核心板上搭建 NIOS II 软核并运行 UCOS II操作系统为例介绍SOPC的开发流程. 第一步:建 ...

  8. vc和halcon数据的相互赋值

    // HTuple→VC 数据类型转换 HTuple hTuple = 1234; int i = hTuple[0].I(); // i=1234 long l = hTuple[0].L(); / ...

  9. js 操作cookie cookie路径问题

    这里主要不是讲这个方法,js写cookie这种代码网上一抓一把,在使用的时候遇到一点问题,就是写的cookie 是有路径问题的,在user目录下可以使用跳转到另外一个目录下cookie,经过比较coo ...

  10. [LeetCode 题解]: String to Interger (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...