where

paddle. where ( condition, x=None, y=None, name=None ) [source]

Return a tensor of elements selected from either $x$ or $y$, depending on $condition$.

Note:

paddle.where(condition) is identical to paddle.nonzero(condition, as_tuple=True).

\[\begin{split}out_i = \begin{cases} x_i, \quad \text{if} \ condition_i \ is \ True \\ y_i, \quad \text{if} \ condition_i \ is \ False \\ \end{cases}\end{split}\]
Parameters
  • condition (Tensor) – The condition to choose x or y. When True(nonzero), yield x, otherwise yield y.

  • x (Tensor or Scalar, optional) – x is a Tensor or Scalar with data type float32, float64, int32, int64. Either both or neither of x and y should be given.

  • y (Tensor or Scalar, optional) – y is a Tensor or Scalar with data type float32, float64, int32, int64. Either both or neither of x and y should be given.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

A Tensor with the same data dype as x.

Return type

Tensor

Examples

import paddle

x = paddle.to_tensor([0.9383, 0.1983, 3.2, 1.2])
y = paddle.to_tensor([1.0, 1.0, 1.0, 1.0])
out = paddle.where(x>1, x, y)

print(out)
#out: [1.0, 1.0, 3.2, 1.2]

out = paddle.where(x>1)
print(out)
#out: (Tensor(shape=[2, 1], dtype=int64, place=CPUPlace, stop_gradient=True,
#            [[2],
#             [3]]),)