PyTorch torch.roll 函数
torch.roll 是 PyTorch 中用于滚动张量元素的函数。
函数定义
torch.roll(input, shifts, dims)
使用示例
实例
import torch
x = torch.arange(8)
# 向前滚动 2 位
print(torch.roll(x, 2))
# 向后滚动 2 位
print(torch.roll(x, -2))
x = torch.arange(8)
# 向前滚动 2 位
print(torch.roll(x, 2))
# 向后滚动 2 位
print(torch.roll(x, -2))
输出结果为:
tensor([6, 7, 0, 1, 2, 3, 4, 5]) tensor([2, 3, 4, 5, 6, 7, 0, 1])

Pytorch torch 参考手册