首页 > 编程语言 > 详细

吴裕雄 python 神经网络——TensorFlow pb文件保存方法

时间:2019-05-16 14:51:17      阅读:162      评论:0      收藏:0      [点我收藏+]
import tensorflow as tf
from tensorflow.python.framework import graph_util

v1 = tf.Variable(tf.constant(1.0, shape=[1]), name = "v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name = "v2")
result = v1 + v2

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    graph_def = tf.get_default_graph().as_graph_def()
    output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, [add])
    with tf.gfile.GFile("E:\\Saved_model\\combined_model.pb", "wb") as f:
           f.write(output_graph_def.SerializeToString())

技术分享图片

from tensorflow.python.platform import gfile

with tf.Session() as sess:
    model_filename = "E:\\Saved_model\\combined_model.pb"
   
    with gfile.FastGFile(model_filename, rb) as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    result = tf.import_graph_def(graph_def, return_elements=["add:0"])
    print(sess.run(result))

技术分享图片

 

吴裕雄 python 神经网络——TensorFlow pb文件保存方法

原文:https://www.cnblogs.com/tszr/p/10875157.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!