mindspore.Tensor.take

Tensor.take(indices, axis=None, mode='clip')[source]

Takes elements from a tensor along an axis.

Parameters
  • indices (Tensor) – The indices with shape (Nj…) of the values to extract.

  • axis (int, optional) – The axis over which to select values. By default, the flattened input tensor is used. Default: None.

  • mode ('raise', 'wrap', 'clip', optional) –

    • raise: Raises an error;

    • wrap: Wraps around;

    • clip: Clips to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

    Default: ‘clip’.

Returns

Tensor, the indexed result.

Raises

ValueError – If axis is out of range, or mode has values other than (‘raise’, ‘wrap’, ‘clip’)

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.array([4, 3, 5, 7, 6, 8]))
>>> indices = Tensor(np.array([0, 1, 4]))
>>> output = a.take(indices)
>>> print(output)
[4 3 6]