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"
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
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
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
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
相关文章推荐
- How to use write and run MapReduce in eclipse on windows.
- How to use boost::property_tree to load and write JSON
- how to write your annotation types and make use of built-in annotations to control their behavior
- How to use java Properties API -- read and write
- How to use calltree and Graphviz/CodeViz
- Kernel Korner - Why and How to Use Netlink Socket
- How To Install and Use the Web Application Stress (WAS) Tool
- What is SPAWN concurrent program and how to write such program?
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)
- How To Use Function 'F4_FILENAME' and 'KD_GET_FILENAME_ON_F4'
- About:How to write the declaration of pointers and references rightly.
- Kernel Korner - Why and How to Use Netlink Socket
- Kernel Korner - Why and How to Use Netlink Socket
- How to Create and Use the DLL
- Create intelligent Web spiders -How to use Java network objects and HTML objects
- (Page 2 of 3 )A walking tour of JavaBeans 2 :What JavaBeans is, how it works, and why you want to use it
- (Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- how_to_use_ant_query_data_and_mail
- How to create a DLL library in C and then use it with C#