mindspore.Tensor.select

Tensor.select(condition, y)[source]

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

It can be defined as:

\[\begin{split}out_i = \begin{cases} tensor_i, & \text{if } condition_i \\ y_i, & \text{otherwise} \end{cases}\end{split}\]
Parameters
  • condition (Tensor[bool]) – The condition tensor, decides which element is chosen. The shape is the same as the current Tensor.

  • y (Union[Tensor, int, float]) – If y is Tensor, the shape is the same as the current Tensor. 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 the Tensor.

Returns

Tensor, has the same shape as the current Tensor.

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

  • ValueError – The shapes of inputs are different.

Supported Platforms:

Ascend GPU CPU

Examples

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