张量流是懒惰的吗?

Is tensorflow lazy?

假设您有一段这样的代码

import tensorflow as tf
...
f = h*y + z*t  #Just some expression involving other tensors.
e = ... # some expression that does not involve f. 
result = tf.select(b, e, f)

sess.run(result)

b 是一个与 e 和 f 形状相同的布尔张量。 如果 b 的所有元素都评估为真,我们不需要 f 并且结果将只是(或等于)e。

问题:当session为运行,结果,且e的元素都为真时,f是否被求值?

TL;DR: TensorFlow 是严格的,因此 ef 都将在 tf.select() 节点执行之前进行评估。

这引起了一些混乱。 TensorFlow 首先根据生成获取的值(即 sess.run() 的参数)静态 需要哪些操作来修剪数据流图。然而,一旦图形被修剪,运行时将使用严格执行,因此必须在操作执行之前计算操作(例如 tf.select())的所有输入。

tf.control_flow_ops module, using the tf.control_flow_ops.cond() 函数中有对条件执行的实验性支持,但目前很少有记录。