mindspore.Tensor.narrow

Tensor.narrow(axis, start, length)[source]

Returns a narrowed tensor from input tensor. The dimension axis is input from start to start + length.

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

  • start (int) – the starting dimension.

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

Returns

Tensor.

  • output (Tensors) - The narrowed tensor.

Raises
  • TypeError – axis is not integer.

  • TypeError – start is not integer.

  • TypeError – length is not integer.

  • ValueError – axis is not in the range of [0, ndim-1].

  • ValueError – start is not in the range of [0, shape[axis]-1].

  • ValueError – start+length is greater than shape[axis]-1.

Supported Platforms:

Ascend GPU CPU

Examples

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