mindspore.Tensor.select

Tensor.select(dim, index) Tensor[source]

Slices the self tensor along the selected dimension at the given index.

Warning

Non-backward-compatible change after version 2.9.0: overload select(condition, y) will be removed, and only select(dim, index) will be supported.

Parameters
  • dim (int) – the dimension to slice.

  • index (int) – the index to select with.

Returns

Tensor.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> from mindspore import Tensor
>>> input = Tensor([[2, 3, 4, 5], [3, 2, 4, 5]])
>>> y = Tensor.select(input, 0, 0)
>>> print(y)
[2 3 4 5]
Tensor.select(condition, y) Tensor[source]

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

It can be defined as:

\[\begin{split}out_i = \begin{cases} self_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 \((x_1, x_2, ..., x_N, ..., x_R)\).

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

Returns

Tensor, has the same shape as condition.

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

  • ValueError – The shape of inputs cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor
>>> # Both input are Tensor
>>> cond = Tensor([True, False])
>>> x = Tensor([2,3], mindspore.float32)
>>> y = Tensor([1,2], mindspore.float32)
>>> output = Tensor.select(x, cond, y)
>>> print(output)
[2. 2.]