tensorflow从0开始(6)——保存加载模型
来源:互联网 发布:淘宝账户名怎么修改 编辑:程序博客网 时间:2024/06/06 09:30
目的
学习tensorflow的目的是能够训练的模型,并且利用已经训练好的模型对新数据进行预测。下文就是一个简单的保存模型加载模型的过程。
保存模型
import tensorflow as tfimport osimport numpy as npfrom tensorflow.python.platform import gfileflags = tf.app.flagsFLAGS = flags.FLAGSflags.DEFINE_string('summaries_dir', '/tmp/save_graph_logs', 'Summaries directory')data = np.arange(10,dtype=np.int32)with tf.Session() as sess: print("# build graph and run") input1= tf.placeholder(tf.int32, [10], name="input") output1= tf.add(input1, tf.constant(100,dtype=tf.int32), name="output") # data depends on the input data saved_result= tf.Variable(data, name="saved_result") do_save=tf.assign(saved_result,output1) tf.initialize_all_variables() os.system("rm -rf /tmp/save_graph_logs") merged = tf.merge_all_summaries() train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir, sess.graph) os.system("rm -rf /tmp/load") tf.train.write_graph(sess.graph_def, "/tmp/load", "test.pb", False) #proto # now set the data: result,_=sess.run([output1,do_save], {input1: data}) # calculate output1 and assign to 'saved_result' saver = tf.train.Saver(tf.all_variables()) saver.save(sess,"checkpoint.data")
模型图示
加载模型
with tf.Session() as persisted_sess: print("load graph") with gfile.FastGFile("/tmp/load/test.pb",'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) persisted_sess.graph.as_default() tf.import_graph_def(graph_def, name='') print("map variables") persisted_result = persisted_sess.graph.get_tensor_by_name("saved_result:0") tf.add_to_collection(tf.GraphKeys.VARIABLES,persisted_result) try: saver = tf.train.Saver(tf.all_variables()) # 'Saver' misnomer! Better: Persister! except:pass print("load data") saver.restore(persisted_sess, "checkpoint.data") # now OK print(persisted_result.eval()) print("DONE")
显示结果
0 0
- tensorflow从0开始——保存加载模型
- tensorflow从0开始(6)——保存加载模型
- tensorflow从0开始(6)——保存加载模型
- tensorflow-模型保存和加载(二)
- tensorflow保存 和 加载模型
- tensorflow 模型保存与加载
- Tensorflow 保存和加载模型
- TensorFlow 模型保存与加载
- tensorflow保存和加载模型
- tensorflow从0开始(5)——Python相关
- tensorflow之保存模型与加载模型
- tensorflow从0开始(7)——利用tensorflow进行开发的准备工作
- TensorFlow学习笔记(2)——保存和加载训练模型参数
- tensorflow从0开始
- tensorflow 保存和加载模型 -2
- tensorflow模型参数保存和加载问题
- TensorFlow保存和加载训练模型
- tensorflow保存加载模型查看训练参数
- 关于VC2010工程中的ipch文件夹和sdf文件
- VB6 异常捕捉 Try catch
- numpy.newaxis
- 11Spring的事务处理
- hdu2049 不容易系列之(4)——考新郎
- tensorflow从0开始(6)——保存加载模型
- Medium 5题 Longest Palindromic Substring
- 12Spring的事务处理二
- java算法4~交换两个整数,不能使用任何额外的变量
- PHP 用户提交的数据的安全性验证
- python中is和==的区别
- 13Spring的数据访问方式
- spring-data-mongodb-1.9.x中Aggregation关于Conditional Aggregation Operators的坑
- IOS Label下划线 中划线