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

  • 模型保存
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())
  • 模型加载
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,操作就可以了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值