我试图理解以下行为:当我运行代码时
import tensorflow as tf
x = tf.Variable(1.0)
y = tf.Variable(0.0)
f = x*x
op0 = tf.assign_add(x, 1.0)
with tf.control_dependencies([op0]):
op1 = tf.assign(y, f)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(op1)
print(y.eval())
结果有时是4.0
,但有时是{1.0
的结果表明依赖项{4.0
,op0
一定是经过计算的,据我所知,这可能是由依赖关系触发的。在
如果我做类似的事情,但是没有张量f
,例如
结果总是2.0
如预期。在
有人能解释一下为什么第二种情况下的行为是不同的,以及在第一种情况下,x
更新后,我如何执行f
的评估?在
您必须确保在第一个赋值发生后计算
f
。所以:我想我找到了解决办法。在我的应用程序中,我实际计算的是}本身,因此下面的方法似乎有效:
f
的梯度,而不是{相关问题 更多 >
编程相关推荐