【tensorflow】tf-tf.where(condition, x, y, name)

def where_v2(condition, x=None, y=None, name=None):

  1. 参数只有condition;
tf.where([True,False,False,True])
return: <tf.Tensor:shape=(2,1),dtype=int64,
numpy=array([[0],[3]])
tf.where([[True,False],[False,True]])*

tf.Tensor:shape=(2,2),dtype=int64,
numpy=array([[0,0],[1,1]])>
tf.where([[[True,False],[False,True],[True,True]]])
tf.Tensor:shape=(4,3),dtype=int64,
numpy=array([[0,0,0],[0,1,1],[0,2,0],[0,2,1]])

Theconditiontensor actssa mask that chooses whether the corresponding

element/row in the output should be taken fromx

(if the elemment inconditionisTrue) ory`(if it is false).

*存在条件的时候如果条件是true,返回x,*否则返回y

 tf.where([True, False, False, True], [1,2,3,4], [100,200,300,400])
  <tf.Tensor: shape=(4,), dtype=int32, numpy=array([  1, 200, 300,   4],
  dtype=int32)

两个数组[1, 2, 3, 4] and [100, 200, 300, 400]
对照条件[True, False, False, True],可知条件数组为True下标为[0, 3] 所以选择x中的元素1和元素4,剩下从y矩阵选择;

tf.where([True, False, False, True], 1, 100)
  <tf.Tensor: shape=(4,), dtype=int32, numpy=array([  1, 100, 100,   1],
  dtype=int32)>
posted @ 2022-05-02 19:41  jucw  阅读(38)  评论(0编辑  收藏  举报