PyTorch torch.roll function
PyTorch torch reference manual
torch.roll is a function in PyTorch used to roll tensor elements.
Function definition
torch.roll(input, shifts, dims)
Usage examples
Example
import
torch
x
=
torch.
arange
(
8
)
Roll forward by 2 positions
(
torch.
roll
(
x
,
2
)
)
Roll backward by 2 positions
(
torch.
roll
(
x
,
2
)
)
The output is:
tensor([6, 7, 0, 1, 2, 3, 4, 5])
tensor([2, 3, 4, 5, 6, 7, 0, 1])