tf.where的函数调用为
tf.where(condition, x = None, y = None, name = None)
根据condition判定返回, condition是True,选择x;condition是False,选择y。
换句话说condition中元素为True的元素替换为x中的元素,为False的元素替换为y中对应元素, x只负责对应替换True的元素,y只负责对应替换False的元素,x,y各有分工.
例如:
import tensorflow as tf
x = [[1,2,3],[4,5,6]]
y = [[7,8,9],[10,11,12]]
condition3 = [[True,False,False],
[False,True,True]]
condition4 = [[True,False,False],
[True,True,False]]
with tf.Session() as sess:
print(sess.run(tf.where(condition3,x,y)))
print(sess.run(tf.where(condition4,x,y)))
输出结果:
1, [[ 1 8 9]
[10 5 6]]
2, [[ 1 8 9]
[ 4 5 12]]