mindspore.ops.roll

mindspore.ops.roll(input, shifts, dims=None)[source]

Rolls the elements of a tensor along an axis.

Parameters
  • input (Tensor) – Input tensor.

  • shifts (Union[list(int), tuple(int), int]) – Specifies the number of places by which elements are shifted positively (towards larger indices) along the specified dimension. Negative shifts will roll the elements in the opposite direction.

  • dims (Union[list(int), tuple(int), int], optional) – Specifies the dimension indexes of shape to be rolled. Default: None. If dims is None, the Tensor will be flattened before rolling and then restored to the original shape.

Returns

Tensor, has the same shape and type as input.

Raises
  • TypeError – If shifts is not an int, a tuple or a list.

  • TypeError – If dims is not an int, a tuple or a list.

Supported Platforms:

GPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import ops
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([0, 1, 2, 3, 4]).astype(np.float32))
>>> output = ops.roll(input_x, shifts=2, dims=0)
>>> print(output)
[3. 4. 0. 1. 2.]