where(condition, x=None, y=None, name=None)的用法
condition, x, y 相同维度,condition是bool型值,True/False
返回值是对应元素,condition中元素为True的元素替换为x中的元素,为False的元素替换为y中对应元素
x只负责对应替换True的元素,y只负责对应替换False的元素,x,y各有分工
由于是替换,返回值的维度,和condition,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]]
这篇博客介绍了TensorFlow中tf.where()函数的用法,它根据给定的条件(condition)来选择从x或y中选取元素。当condition为True时,选取x对应的元素;为False时,选取y对应的元素。通过示例展示了如何在会话中运行tf.where()并打印结果。
4994

被折叠的 条评论
为什么被折叠?



