快捷方式

torch.roll

torch.roll(input, shifts, dims=None) Tensor

沿给定维度滚动张量 input。移出最后位置的元素将重新从第一位置引入。如果 dimsNone,则在滚动前将张量展平,然后恢复到原始形状。

参数
  • input (Tensor) – 输入张量。

  • shifts (intinttuple) – 元素移动的位数。如果 shifts 是一个 tuple,则 dims 必须是相同大小的 tuple,并且每个维度将按对应的值进行滚动

  • dims (intinttuple) – 沿哪个轴进行滚动

示例

>>> x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2)
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])
>>> torch.roll(x, 1)
tensor([[8, 1],
        [2, 3],
        [4, 5],
        [6, 7]])
>>> torch.roll(x, 1, 0)
tensor([[7, 8],
        [1, 2],
        [3, 4],
        [5, 6]])
>>> torch.roll(x, -1, 0)
tensor([[3, 4],
        [5, 6],
        [7, 8],
        [1, 2]])
>>> torch.roll(x, shifts=(2, 1), dims=(0, 1))
tensor([[6, 5],
        [8, 7],
        [2, 1],
        [4, 3]])

文档

查阅 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并解答您的疑问

查看资源