Tensorflow深度学习之三十:tf.where()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DaVinciL/article/details/82533280

一、简介
Return the elements, either from x or y, depending on the condition.

If both x and y are None, then this operation returns the coordinates of true elements of condition.The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true elements, and the second dimension (columns) represents the coordinates of the true elements. Keep in mind, the shape of the output tensor can vary depending on how many true values there are in input. Indices are output in row-major order.

If both non-None, x and y must have the same shape. The condition tensor must be a scalar if x and y are scalar. If x and y are vectors of higher rank, then condition must be either a vector with size matching the first dimension of x, or must have the same shape as x.

The condition tensor acts as a mask that chooses, based on the value at each element, whether the corresponding element / row in the output should be taken from x (if true) or y (if false).

If condition is a vector and x and y are higher rank matrices, then it chooses which row (outer dimension) to copy from x and y. If condition has the same shape as x and y, then it chooses which element to copy from x and y.

翻译:
根据condition 提供的信息,从x 或者y 返回对应的元素。
如果xy 都为None,该函数返回参数condition 中true元素的坐标,这些坐标信息以二位数组(tensor)的形式返回,第一维(行)代表condition 参数中true元素的数目,第二维(列)表示true元素的坐标。输出tensor的shape会根据condition 参数中true元素的数目变化。
如果xy 都不为None,xy 的shape必须相同,如果xy 都为一个标量,则condition 也必须是一个标量。如果xy 是一个拥有更高的秩的向量,那么condition 必须是大小与x的第一维匹配的向量,或者必须具有与x相同的形状。
condition 充当一个掩码(mask),它根据每个元素的值选择输出中的相应元素/行是否应取自x(如果为true)或y(如果为false)。
如果condition是一个向量而xy 是更高级别的矩阵,那么它选择从xy复制哪一行(外部维度)。如果condition具有与xy相同的形状,那么它选择从xy复制哪个元素。

二、参数

参数
condition A Tensor of type bool 一个bool 类型的tensor
x A Tensor which may have the same shape as condition. If condition is rank 1, x may have higher rank, but its first dimension must match the size of condition. condition具有相同的形状一个tensor。 如果condition是等级1,x可能有更高的rank,但它的第一个维度必须与condition的大小相匹配。
y A tensor with the same shape and type as x. 具有与“x”相同形状和类型的tensor
name A name of the operation (optional) 名称,可选参数

三、代码

import tensorflow as tf
import numpy as np

# 定义一个tensor,内部数据随机产生
a = tf.convert_to_tensor(np.random.random([4, 5]), dtype=tf.float32)

# 返回大于0.5的数值的坐标
b = tf.where(a > 0.5)

with tf.Session() as sess:
    print("tensor:\n", sess.run(a))
    print("coordinates:\n", sess.run(b))

运行结果:

tensor:
 [[0.33903903 0.1701065  0.38461614 0.02950973 0.42442432]
 [0.07991146 0.4968791  0.93727195 0.47402245 0.88493776]
 [0.73473597 0.72057265 0.7832902  0.6842829  0.8187402 ]
 [0.4326151  0.04389747 0.06952374 0.49539143 0.11805743]]
coordinates:
 [[1 2]
 [1 4]
 [2 0]
 [2 1]
 [2 2]
 [2 3]
 [2 4]]
import tensorflow as tf
import numpy as np

# 定义一个tensor,表示condition,内部数据随机产生
condition = tf.convert_to_tensor(np.random.random([5]), dtype=tf.float32)

# 定义两个tensor,表示原数据
a = tf.ones(shape=[5, 3], name='a')

b = tf.zeros(shape=[5, 3], name='b')

# 选择大于0.5的数值的坐标,并根据condition信息在a和b中选取数据
result = tf.where(condition > 0.5, a, b)

with tf.Session() as sess:
    print("condition:\n", sess.run(condition))
    print("result:\n", sess.run(result))

运行结果:

condition:
 [0.37309206 0.2993927  0.23316757 0.8938935  0.16948673]
result:
 [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [1. 1. 1.]
 [0. 0. 0.]]

猜你喜欢

转载自blog.csdn.net/DaVinciL/article/details/82533280