我试图弄清楚在TensorFlow的构建阶段进行函数调用时会发生什么,并创建了以下程序
import tensorflow as tf
def fun1(x, y):
print "we are inside the function now"
z = x + y
return z
x = tf.placeholder(tf.float32, shape=(), name="x")
y = tf.placeholder(tf.float32, shape=(), name="y")
z = fun1(x, y)
init = tf.global_variables_initializer()
with tf.Session() as sess:
print "start a session"
sess.run(init)
print "just initialized all variables"
val = z.eval(feed_dict={x:3, y:4})
print val
我得到的结果是:
start a session
just initialized all variables
7.0
所以fun1似乎没有真正运行,因为它没有打印“我们现在在函数内部”。那么TensorFlow在构造阶段对函数调用到底做了什么呢?