torch.roll() 详解
生活随笔
收集整理的這篇文章主要介紹了
torch.roll() 详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
torch.roll(input,?shifts,?dims=None)
-
input?(Tensor) – the input tensor.
-
shifts?(int?or?tuple of python:ints) – The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value
-
dims?(int?or?tuple of python:ints) – Axis along which to roll
?
-
input?(Tensor) –輸入tensor
-
shifts?(int?or?tuple of python:ints) – 變換的幅度,為整數或者元組。若為元組,其shape與dims保持一樣
-
dims?(int?or?tuple of python:ints) – 維度。在dims維上進行大小為shift的變換。0 為縱向,1為橫向
?
代碼示例:?
?
import torchx = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]).view(3, 3) print(x, '\n') #tensor([[1, 2, 3], # [4, 5, 6], # [7, 8, 9]])print(1) print(torch.roll(x, (-1 , -1), (0 , 0)), '\n') #tensor([[7, 8, 9], # [1, 2, 3], # [4, 5, 6]])print(2) print(torch.roll(x, (-1 , 0), (0 , 1)), '\n') #tensor([[4, 5, 6], # [7, 8, 9], # [1, 2, 3]])print(3) print(torch.roll(x, (-1 , 1), (1 , 0)), '\n') #tensor([[8, 9, 7], # [2, 3, 1], # [5, 6, 4]])print(4) print(torch.roll(x, (0 , -1), (1 , 1)), '\n') #tensor([[2, 3, 1], # [5, 6, 4], # [8, 9, 7]])print(5) print(torch.roll(x, (0 , 0), (1 , 1)), '\n') #tensor([[1, 2, 3], # [4, 5, 6], # [7, 8, 9]])print(6) print(torch.roll(x, (0 , 1), (1 , 1)), '\n') #tensor([[3, 1, 2], # [6, 4, 5], # [9, 7, 8]])print(7) print(torch.roll(x, (1 , -1), (1 , 0)), '\n') #tensor([[6, 4, 5], # [9, 7, 8], # [3, 1, 2]])print(8) print(torch.roll(x, (1 , 0), (0 , 1)), '\n') #tensor([[7, 8, 9], # [1, 2, 3], # [4, 5, 6]])print(9) print(torch.roll(x, (1 , 1), (0 , 0)), '\n') #tensor([[4, 5, 6], # [7, 8, 9], # [1, 2, 3]])x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2) print(x, '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, -3, 1), '\n') #tensor([[2, 1], # [4, 3], # [6, 5], # [8, 7]])print(torch.roll(x, -2, 1), '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, -1, 1), '\n') #tensor([[2, 1], # [4, 3], # [6, 5], # [8, 7]])print(torch.roll(x, 0, 1), '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, 1, 1), '\n') #tensor([[2, 1], # [4, 3], # [6, 5], # [8, 7]])print(torch.roll(x, 2, 1), '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, 3, 1), '\n') #tensor([[2, 1], # [4, 3], # [6, 5], # [8, 7]])print(torch.roll(x, -3, 0), '\n') #tensor([[7, 8], # [1, 2], # [3, 4], # [5, 6]])print(torch.roll(x, -2, 0), '\n') #tensor([[5, 6], # [7, 8], # [1, 2], # [3, 4]])print(torch.roll(x, -1, 0), '\n') #tensor([[3, 4], # [5, 6], # [7, 8], # [1, 2]])print(torch.roll(x, 0, 0), '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, 1, 0), '\n') #tensor([[7, 8], # [1, 2], # [3, 4], # [5, 6]])print(torch.roll(x, 2, 0), '\n') #tensor([[5, 6], # [7, 8], # [1, 2], # [3, 4]])print(torch.roll(x, 3, 0), '\n') #tensor([[3, 4], # [5, 6], # [7, 8], # [1, 2]])print(torch.roll(x, -3), '\n') #tensor([[4, 5], # [6, 7], # [8, 1], # [2, 3]])print(torch.roll(x, -2), '\n') #tensor([[3, 4], # [5, 6], # [7, 8], # [1, 2]])print(torch.roll(x, -1), '\n') #tensor([[2, 3], # [4, 5], # [6, 7], # [8, 1]])print(torch.roll(x, 0), '\n') #tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]])print(torch.roll(x, 1), '\n') #tensor([[8, 1], # [2, 3], # [4, 5], # [6, 7]])print(torch.roll(x, 2), '\n') #tensor([[7, 8], # [1, 2], # [3, 4], # [5, 6]])print(torch.roll(x, 3), '\n') #tensor([[6, 7], # [8, 1], # [2, 3], # [4, 5]])?
?
參考:https://pytorch.org/docs/master/generated/torch.roll.html?
?
總結
以上是生活随笔為你收集整理的torch.roll() 详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 列表赋值操作可能存在的潜在
- 下一篇: 求列表中个元素的数量