TensorFlow实现保存训练模型为pd文件并恢复
更新时间:2020年02月06日 14:26:29 作者:CrazyStoneZw
今天小编就为大家分享一篇TensorFlow实现保存训练模型为pd文件并恢复,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
TensorFlow保存模型代码
import tensorflow as tf from tensorflow.python.framework import graph_util var1 = tf.Variable(1.0, dtype=tf.float32, name='v1') var2 = tf.Variable(2.0, dtype=tf.float32, name='v2') var3 = tf.Variable(2.0, dtype=tf.float32, name='v3') x = tf.placeholder(dtype=tf.float32, shape=None, name='x') x2 = tf.placeholder(dtype=tf.float32, shape=None, name='x2') addop = tf.add(x, x2, name='add') addop2 = tf.add(var1, var2, name='add2') addop3 = tf.add(var3, var2, name='add3') initop = tf.global_variables_initializer() model_path = './Test/model.pb' with tf.Session() as sess: sess.run(initop) print(sess.run(addop, feed_dict={x: 12, x2: 23})) output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['add', 'add2', 'add3']) # 将计算图写入到模型文件中 model_f = tf.gfile.FastGFile(model_path, mode="wb") model_f.write(output_graph_def.SerializeToString())
读取模型代码
import tensorflow as tf with tf.Session() as sess: model_f = tf.gfile.FastGFile("./Test/model.pb", mode='rb') graph_def = tf.GraphDef() graph_def.ParseFromString(model_f.read()) c = tf.import_graph_def(graph_def, return_elements=["add2:0"]) c2 = tf.import_graph_def(graph_def, return_elements=["add3:0"]) x, x2, c3 = tf.import_graph_def(graph_def, return_elements=["x:0", "x2:0", "add:0"]) print(sess.run(c)) print(sess.run(c2)) print(sess.run(c3, feed_dict={x: 23, x2: 2}))
以上这篇TensorFlow实现保存训练模型为pd文件并恢复就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
使用Python快速打开一个百万行级别的超大Excel文件的方法
这篇文章主要介绍了使用Python快速打开一个百万行级别的超大Excel文件的方法,本文通过实例代码给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-03-03python except异常处理之后不退出,解决异常继续执行的实现
这篇文章主要介绍了python except异常处理之后不退出,解决异常继续执行的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-04-04Django模板标签{% for %}循环,获取制定条数据实例
这篇文章主要介绍了Django模板标签{% for %}循环,获取制定条数据实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-05-05Django如何使用asyncio协程和ThreadPoolExecutor多线程
这篇文章主要介绍了Django如何使用asyncio协程和ThreadPoolExecutor多线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-10-10python 读取yaml文件的两种方法(在unittest中使用)
这篇文章主要介绍了python 读取yaml文件的两种方法(在unittest中使用),帮助大家更好的理解和学习python,感兴趣的朋友可以了解下2020-12-12
最新评论