【tensorflow】tf-tf.where(condition, x, y, name)
def where_v2(condition, x=None, y=None, name=None):
- 参数只有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]])
Thecondition
tensor actssa mask that chooses whether the corresponding
element/row in the output should be taken fromx
(if the elemment inconditionisTrue) or
y`(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)>
本文来自博客园,作者:jucw,转载请注明原文链接:https://www.cnblogs.com/Jucw/p/16216572.html