【发布时间】:2016-05-06 03:54:53
【问题描述】:
我的图表中需要一个条件控制流。如果pred 是True,则图应该调用一个更新变量然后返回它的操作,否则它返回变量不变。一个简化的版本是:
pred = tf.constant(True)
x = tf.Variable([1])
assign_x_2 = tf.assign(x, [2])
def update_x_2():
with tf.control_dependencies([assign_x_2]):
return tf.identity(x)
y = tf.cond(pred, update_x_2, lambda: tf.identity(x))
with tf.Session() as session:
session.run(tf.initialize_all_variables())
print(y.eval())
但是,我发现pred=True 和pred=False 导致相同的结果y=[2],这意味着当update_x_2 未被tf.cond 选择时,也会调用assign 操作。这要怎么解释?以及如何解决这个问题?
【问题讨论】:
标签: tensorflow