初始化tf.Session()时,我们可以传递一个像tf.Session(graph=my_graph)这样的图形,例如:
import tensorflow as tf
# define graph
my_graph = tf.Graph()
with my_graph.as_default():
a = tf.constant(100., tf.float32, name='a')
# run graph
with tf.Session(graph=my_graph) as sess:
a = sess.graph.get_operation_by_name('a')
print(sess.run(a)) # prints None在上面的示例中,它打印None。如何执行在my_graph中定义的操作?
发布于 2016-11-12 00:30:49
这就是我想要的行为,但我明白为什么会令人惊讶!下面的行返回一个tf.Operation对象:
a = sess.graph.get_operation_by_name('a')...and当您将一个tf.Operation对象传递给Session.run()时,TensorFlow将执行该操作,但它将放弃其输出并返回None。
通过显式指定该操作的第0输出并检索tf.Tensor对象,下面的程序可能具有您期望的行为:
with tf.Session(graph=my_graph) as sess:
a = sess.graph.get_operation_by_name('a').outputs[0]
# Or you could do:
# a = sess.graph.get_tensor_by_name('a:0')
print(sess.run(a)) # prints '100.'https://stackoverflow.com/questions/40558238
复制相似问题