TensorFlow实现保存训练模型为pd文件并恢复
作者:CrazyStoneZw 时间:2021-03-01 05:45:27
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}))
来源:https://blog.csdn.net/oYouHuo/article/details/81111833
标签:TensorFlow,训练模型,pd,恢复
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server数据库备份多种方法
2008-11-24 15:27:00
Bootstrap响应式侧边栏改进版
2023-08-17 02:26:10
![](https://img.aspxhome.com/file/2023/1/56051_0s.jpg)
SQL Server跟踪数据实现索引优化向导
2009-02-13 17:14:00
![](https://img.aspxhome.com/file/UploadPic/20092/2009216122820265.jpg)
轻松解决SQL Server 2005中的常见问题
2008-11-28 14:11:00
php strftime函数获取日期时间(switch用法)
2023-06-11 13:26:33
提高javascript的速度系列(序)
2008-07-15 12:38:00
php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案
2023-09-27 17:22:32
asp如何修改WINNT的登录密码?
2010-06-10 17:06:00
界面设计10条可用性方面的启发
2010-04-06 17:22:00
![](https://img.aspxhome.com/file/UploadPic/20104/6/heuristics-95s.gif)
Oracle常用dump命令,记录一下备查。
2009-03-04 10:27:00
SQLserver中字符串查找功能patindex和charindex的区别
2012-06-06 20:20:42
php获取数组长度的方法(有实例)
2023-11-20 07:55:28
Python3爬虫中关于中文分词的详解
2023-08-12 23:10:28
Css Reset(复位)整理
2008-09-09 21:58:00
pyecharts如何实现显示数据为百分比的柱状图
2021-06-27 17:11:52
![](https://img.aspxhome.com/file/2023/4/72134_0s.png)
跨浏览器的本地存储(一):userData behavior
2008-08-05 18:13:00
golang 微服务之gRPC与Protobuf的使用
2023-06-17 20:36:03
用css3-tranistions实现平滑过渡
2009-12-23 19:24:00
Coda 前端开发插件 F2E Tools
2009-09-22 14:53:00
![](https://img.aspxhome.com/file/UploadPic/20099/22/coda-f2etools-66s.png)
基于python中staticmethod和classmethod的区别(详解)
2023-09-30 22:03:32