评价此页

torch.roll#

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

将张量 input 沿着给定的维度进行滚动。被移到最后一个位置之外的元素会重新引入到第一个位置。如果 dimsNone,张量将在滚动前被展平,然后恢复到原始形状。

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

  • shifts (inttuple of ints) – 张量元素的移位量。如果 shifts 是一个元组,dims 必须是相同大小的元组,并且每个维度将按相应的值进行滚动

  • dims (inttuple of ints) – 滚动的轴

示例

>>> 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]])