快捷方式

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 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得您的问题解答

查看资源