您的位置:首页 > 大数据 > 人工智能

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

2016-11-21 18:37 513 查看
如何寫圖,讀圖。
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  深度学习