mindspore.Tensor.choose

View Source On Gitee
Tensor.choose(choices, mode='clip')[source]

Construct a tensor from an index tensor and a list of tensors to choose from.

Parameters
  • choices (Union[tuple, list, Tensor]) – Choice tensors. The input tensor and all of the choices must be broadcasted to the same shape. If choices is itself a tensor, then its outermost dimension (i.e., the one corresponding to choices.shape[0]) is taken as defining the “sequence”.

  • mode (str, optional) –

    Specifies how indices outside [0, n-1] will be treated. Support 'raise', 'wrap', 'clip'.

    • raise: Raises an error;

    • wrap: Wraps around;

    • clip: Clips to the range. The values greater than n-1 will be mapped to n-1. Note that this mode disables indexing with negative numbers.

    Default: 'clip'.

Returns

Tensor, the merged result.

Raises

ValueError – If the input tensor and any of the choices cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]
>>> x = Tensor(np.array([2, 3, 1, 0]))
>>> print(x.choose(choices))
[20 31 12  3]