tensorflow Using if condition inside the TensorFlow graph with tf.cond When f1 and f2 return multiple tensors


Example

The two functions fn1 and fn2 can return multiple tensors, but they have to return the exact same number and types of outputs.

x = tf.constant(1.)
bool = tf.constant(True)

def fn1():
    return tf.add(x, 1.), x

def fn2():
    return tf.add(x, 10.), x

res1, res2 = tf.cond(bool, fn1, fn2)
# tf.cond returns a list of two tensors
# sess.run([res1, res2]) will return [2., 1.]