mindspore.ops.select

mindspore.ops.select(condition, input, other)[source]

The conditional tensor determines whether the corresponding element in the output must be selected from input (if True) or other (if False) based on the value of each element.

It can be defined as:

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

  • input (Union[Tensor, int, float]) – The first Tensor to be selected. If input is a Tensor, its shape should be or be braodcast to \((x_1, x_2, ..., x_N, ..., x_R)\). If input is int or float, it will be casted to int32 or float32, and broadcast to the same shape as y. There must be at least one Tensor between x and y.

  • other (Union[Tensor, int, float]) – The second Tensor to be selected. If other is a Tensor, its shape should be or be braodcast to \((x_1, x_2, ..., x_N, ..., x_R)\). If other is int or float, it will be casted to int32 or float32, and broadcast to the same shape as y. There must be at least one Tensor between x and y.

Returns

Tensor, has the same shape as condition.

Raises
  • TypeError – If input or other is not a Tensor.

  • ValueError – The shape of inputs cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, ops
>>> # 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.]