mindspore.nn.Roll

class mindspore.nn.Roll(shift, axis)[source]

Rolls the elements of a tensor along an axis.

The elements are shifted positively (towards larger indices) by the offset of shift along the dimension of axis. Negative shift values will shift elements in the opposite direction. Elements that roll passed the last position will wrap around to the first and vice versa. Multiple shifts along multiple axes may be specified.

Parameters
  • shift (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.

  • axis (Union[list(int), tuple(int), int]) – Specifies the dimension indexes of shape to be rolled.

Inputs:
  • input_x (Tensor) - Input tensor.

Outputs:

Tensor, has the same shape and type as input_x.

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

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

  • TypeError – If element of shift is not an int.

  • TypeError – If element of axis is not an int.

  • ValueError – If axis is out of the range [-len(input_x.shape), len(input_x.shape)).

  • ValueError – If length of shape of shift is not equal to length of shape of axis.

Supported Platforms:

Ascend

Examples

>>> input_x = Tensor(np.array([0, 1, 2, 3, 4]).astype(np.float32))
>>> op = nn.Roll(shift=2, axis=0)
>>> output = op(input_x)
>>> print(output)
[3. 4. 0. 1. 2.]
>>> input_x = Tensor(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]).astype(np.float32))
>>> op = nn.Roll(shift=[1, -2], axis=[0, 1])
>>> output = op(input_x)
>>> print(output)
[[7. 8. 9. 5. 6.]
 [2. 3. 4. 0. 1.]]