快捷方式

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 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源