mindspore.Tensor.narrow

Tensor.narrow(dim, start, length) Tensor

Obtains a tensor of a specified length at a specified start position along a specified axis.

Parameters
  • dim (int) – the axis along which to narrow.

  • start (Union[int, Tensor]) – the starting dimension.

  • length (int) – the distance to the ending dimension.

Returns

output (Tensors) - The narrowed tensor.

Raises
  • ValueError – The value of dim is out of range [-input.ndim, input.ndim).

  • ValueError – The value of start is out of range [-input.shape[dim], input.shape[dim]].

  • ValueError – The value of length is out of range [0, input.shape[dim] - start].

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import mint
>>> from mindspore import Tensor
>>> x = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], mindspore.int32)
>>> output = mint.narrow(x, 0, 0, 2)
>>> print(output)
[[ 1 2 3]
 [ 4 5 6]]
>>> output = mint.narrow(x, 1, 1, 2)
>>> print(output)
[[ 2 3]
 [ 5 6]
 [ 8 9]]