mindspore.ops.Select

class mindspore.ops.Select[source]

Returns the selected elements, either from input x or input y, depending on the condition.

Given a tensor as input, this operation inserts a dimension of 1 at the dimension, it was invalid when both math: ‘x’ and math: ‘y’ are none. Keep in mind that the shape of the output tensor can vary depending on how many true values are in the input. Indexes are output in row-first order.

The conditional tensor acts as an optional compensation (mask), which determines whether the corresponding element / row 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:

outi={xi,if conditioniyi,otherwise

If condition is a vector, then x and y are higher-dimensional matrices, then it chooses to copy that row (external dimensions) from x and y. If condition has the same shape as x and y, you can choose to copy these elements from x and y.

Inputs:
  • input_cond (Tensor[bool]) - The shape is (x1,x2,...,xN,...,xR). The condition tensor, decides which element is chosen.

  • input_x (Tensor) - The shape is (x1,x2,...,xN,...,xR). The first input tensor.

  • input_y (Tensor) - The shape is (x1,x2,...,xN,...,xR). The second input tensor.

Outputs:

Tensor, has the same shape as input_x. The shape is (x1,x2,...,xN,...,xR).

Raises
  • TypeError – If input_x or input_y is not a Tensor.

  • ValueError – If shape of input_x is not equal to shape of input_y or shape of input_cond.

Supported Platforms:

Ascend GPU CPU

Examples

>>> select = ops.Select()
>>> input_cond = Tensor([True, False])
>>> input_x = Tensor([2,3], mindspore.float32)
>>> input_y = Tensor([1,2], mindspore.float32)
>>> output = select(input_cond, input_x, input_y)
>>> print(output)
[2. 2.]