2016-11-12 4 views
2

, 우리는 예를 들면, tf.Session(graph=my_graph) 같은 그래프로 전달할 수TensorFlow 세션 실행 그래프()

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 안에 정의 된 연산을 어떻게 실행할 수 있습니까?

답변

8

이것은 의도 한 동작이지만, 나는 그것이 놀라운 이유를 알 수 있습니다! 다음 줄은 tf.Operation 객체 반환

a = sess.graph.get_operation_by_name('a') 

을 ... 그리고 당신이 Session.run()tf.Operation 개체를 전달할 때, 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.' 
관련 문제