mindspore.ops.select

mindspore.ops.select(cond, x, y)[source]

The conditional tensor determines whether the corresponding element in the output must be selected from x (if true) or y (if false) based on the value of each element.

It can be defined as:

\[\begin{split}out_i = \begin{cases} x_i, & \text{if } cond_i \\ y_i, & \text{otherwise} \end{cases}\end{split}\]
Parameters
  • cond (Tensor[bool]) – The condition tensor, decides which element is chosen. The shape is \((x_1, x_2, ..., x_N, ..., x_R)\).

  • x (Union[Tensor, int, float]) – The first Tensor or number to be selected. If x is a Tensor, the shape is or can be broadcadt to \((x_1, x_2, ..., x_N, ..., x_R)\). If x is an int or a float, it will be cast to the type of int32 or float32, and broadcast to the same shape as y. One of x and y must be a Tensor.

  • y (Union[Tensor, int, float]) – The second Tensor or number to be selected. If y is a Tensor, The shape is or can be broadcadt to \((x_1, x_2, ..., x_N, ..., x_R)\). If y is an int or a float, it will be cast to the type of int32 or float32, and broadcast to the same shape as x. One of x and y must be a Tensor.

Returns

Tensor, has the same shape as cond.

Raises
  • TypeError – If x or y is not a Tensor, int or float.

  • ValueError – The shapes of inputs can not be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, ops
>>> # 1) Both inputs are Tensor
>>>
>>> cond = Tensor([True, False])
>>> x = Tensor([2,3], mindspore.float32)
>>> y = Tensor([1,2], mindspore.float32)
>>> output = ops.select(cond, x, y)
>>> print(output)
[2. 2.]
>>> # 2) y is a float
>>> cond = Tensor([True, False])
>>> x = Tensor([2,3], mindspore.float32)
>>> y = 2.0
>>> output = ops.select(cond, x, y)
>>> print(output)
[2. 2.]