前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow(四十七):tensorflow模型持久化

tensorflow(四十七):tensorflow模型持久化

作者头像
ke1th
发布2020-11-24 15:32:12
3250
发布2020-11-24 15:32:12
举报
  • 模型保存
代码语言:javascript
复制
from tensorflow import graph_util
graph_def = tf.get_default_graph().as_graph_def()

# variable 搞成常量节点放到 graph_def 中。并按照 输出 节点进行剪枝
constant_graph = graph_util.convert_variables_to_constants(sess, graph_def, 
                                                           ['dev/variable_root/dev/Sigmoid_1', 
                                                            'dev/variable_root/dev/Sigmoid_2',
                                                           'dev/variable_root/dev/Sum'])
with tf.gfile.FastGFile('./saved_model.pb', mode='wb') as f:
    f.write(constant_graph.SerializeToString())
  • 模型加载
代码语言:javascript
复制
graph = tf.get_default_graph()
model = tf.gfile.FastGFile('path/to/saved_model', 'rb')
graph_def = tf.GraphDef()
graph_def.ParseFromString(model.read())
tf.import_graph_def(graph_def, name='graph') # 用graph_def来构建 tf.Graph
des_ph = graph.get_tensor_by_name("graph/Placeholder:0")
con_ph = graph.get_tensor_by_name("graph/Placeholder_1:0")
cvr_output_tensor = graph.get_tensor_by_name("graph/S1:0")
ucr_output_tensor = graph.get_tensor_by_name("graph/S1S2:0")

# 然后构建session,操作就可以了。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-11-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档