How to use tf.train.write_graph() and tf.import_graph_def()

如何寫圖,讀圖。

https://github.com/tensorflow/tensorflow/issues/616



How to use tf.train.write_graph() and tf.import_graph_def()? It seems that it does not work.

Please refer the following codes and prints, the final result is None, but it is expected to be [101 102 103 104 105 106 107 108 109 110].

code:

import tensorflow as tf
import os
import numpy as np
from tensorflow.python.platform import gfile

input1= tf.placeholder(tf.int32, [10], name="input")
data = np.arange(10)
output1= tf.add(input1, tf.constant(1), name="output")

with tf.Session() as sess:
os.system("rm -rf /tmp/load")
tf.train.write_graph(sess.graph_def, "/tmp/load", "test.pb", False)
print sess.run(output1,{input1:data})
print "Done"

load graph

with gfile.FastGFile("/tmp/load/test.pb",'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')

run

with tf.Session() as sess:
input_x = sess.graph.get_tensor_by_name("input:0")
print input_x
Const = sess.graph.get_tensor_by_name("Const:0")
print Const
output = sess.graph.get_operation_by_name("output")
print output

data1 = np.arange(10)+100
print "data1:", data1
result = sess.run(output, {input_x: data1})
print "result:",result

prints:
[ 1 2 3 4 5 6 7 8 9 10]
Done
Tensor("input:0", shape=TensorShape([Dimension(10)]), dtype=int32)
Tensor("Const:0", shape=TensorShape([]), dtype=int32)
name: "output"
op: "Add"
input: "input"
input: "Const"
attr {
key: "T"
value {
type: DT_INT32
}
}

data1: [100 101 102 103 104 105 106 107 108 109]
result: None


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值