tf.where 应用到了张量的广播机制 张量的广播机制

tf.where(
    condition, x=None, y=None, name=None
)
[if condition[k] =True 取 x[k] else 取 y[k]]
import tensorflow as tf
c=tf.where([True, False, False, True], [1,2,3,4], [100,200,300,400])
print(c)


c=tf.where([True, False, False, True], [1,2,3,4], [100])
print(c)

c=tf.where([True, False, False, True], [1,2,3,4], 100)
print(c)

c=tf.where([True, False, False, True], 1, 100)
print(c)

tf.Tensor([  1 200 300   4], shape=(4,), dtype=int32)
tf.Tensor([  1 100 100   4], shape=(4,), dtype=int32)
tf.Tensor([  1 100 100   4], shape=(4,), dtype=int32)
tf.Tensor([  1 100 100   1], shape=(4,), dtype=int32)