where
- paddle. where ( condition: Tensor, x: Tensor | float | None = None, y: Tensor | float | None = None, name: str | None = None, *, out: Tensor | None = None ) Tensor [source]
-
Return a Tensor of elements selected from either
x
ory
according to corresponding elements ofcondition
. Concretely,\[\begin{split}out_i = \begin{cases} x_i, & \text{if} \ condition_i \ \text{is} \ True \\ y_i, & \text{if} \ condition_i \ \text{is} \ False \\ \end{cases}.\end{split}\]Notes
numpy.where(condition)
is identical topaddle.nonzero(condition, as_tuple=True)
, please refer to nonzero.Note
Alias Support: The parameter name
input
can be used as an alias forx
, andother
can be used as an alias fory
. For example,paddle.where(condition, input=x, other=y)
can be written aspaddle.where(condition, x=x, y=y)
.- Parameters
-
condition (Tensor) – The condition to choose x or y. When True (nonzero), yield x, otherwise yield y, must have a dtype of bool if used as mask.
x (Tensor|scalar|None, optional) – A Tensor or scalar to choose when the condition is True with data type of bfloat16, float16, float32, float64, int32 or int64. Either both or neither of x and y should be given. alias:
input
.y (Tensor|scalar|None, optional) – A Tensor or scalar to choose when the condition is False with data type of bfloat16, float16, float32, float64, int32 or int64. Either both or neither of x and y should be given. alias:
other
.name (str|None, optional) – For details, please refer to api_guide_Name. Generally, no setting is required. Default: None.
out (Tensor|None, optional) – The output tensor. If set, the result will be stored to this tensor. Default is None.
- Returns
-
//www.paddlepaddle.org.cn/documentation/docs/en/develop/guides/advanced/auto_type_promotion_en.html#introduction-to-data-type-promotion>`_).
- Return type
-
Tensor, A Tensor with the same shape as
condition
and same data type asx
andy
. Ifx
andy
have different data types, type promotion rules will be applied (see `Auto Type Promotion <https
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) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, [1. , 1. , 3.20000005, 1.20000005]) >>> out = paddle.where(x>1) >>> print(out) (Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, [2, 3]),)