rot90() can rotate a 2D or more D tensor by 90 degrees as shown below:
*Memos:
-
rot90()
can be used with torch or a tensor. - The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
- The 2nd argument(
int
) withtorch
or the 1st argument(int
) with a tensor isk
(Optional). - The 3rd argument(
tuple
ofint
orlist
ofint
) withtorch
or the 2nd argument(tuple
ofint
orlist
ofint
) with a tensor isdims
(Optional-Default:[0, 1]
).
import torch
my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5]])
torch.rot90(my_tensor)
my_tensor.rot90()
torch.rot90(my_tensor, k=1)
torch.rot90(my_tensor, k=-3)
torch.rot90(my_tensor, dims=(0, 1))
torch.rot90(my_tensor, dims=(0, -1))
torch.rot90(my_tensor, k=1, dims=(0, 1))
torch.rot90(my_tensor, k=1, dims=(0, -1))
torch.rot90(my_tensor, k=1, dims=(-2, 1))
torch.rot90(my_tensor, k=1, dims=(-2, -1))
torch.rot90(my_tensor, k=3, dims=(1, 0))
torch.rot90(my_tensor, k=3, dims=(1, -2))
torch.rot90(my_tensor, k=3, dims=(-1, 0))
torch.rot90(my_tensor, k=3, dims=(-1, -2))
torch.rot90(my_tensor, k=-1, dims=(1, 0))
torch.rot90(my_tensor, k=-1, dims=(1, -2))
torch.rot90(my_tensor, k=-1, dims=(-1, 0))
torch.rot90(my_tensor, k=-1, dims=(-1, -2))
torch.rot90(my_tensor, k=-3, dims=(0, 1))
torch.rot90(my_tensor, k=-3, dims=(0, -1))
torch.rot90(my_tensor, k=-3, dims=(-2, 1))
torch.rot90(my_tensor, k=-3, dims=(-2, -1))
etc.
# tensor([[2, 5], [1, 4], [0, 3]])
torch.rot90(my_tensor, k=0)
torch.rot90(my_tensor, k=-4)
torch.rot90(my_tensor, k=0, dims=(0, 1))
torch.rot90(my_tensor, k=0, dims=(0, -1))
torch.rot90(my_tensor, k=0, dims=(1, 0))
torch.rot90(my_tensor, k=0, dims=(1, -2))
torch.rot90(my_tensor, k=0, dims=(-1, 0))
torch.rot90(my_tensor, k=0, dims=(-1, -2))
torch.rot90(my_tensor, k=0, dims=(-2, 1))
torch.rot90(my_tensor, k=0, dims=(-2, -1))
torch.rot90(my_tensor, k=-4, dims=(0, 1))
torch.rot90(my_tensor, k=-4, dims=(0, -1))
torch.rot90(my_tensor, k=-4, dims=(1, 0))
torch.rot90(my_tensor, k=-4, dims=(1, -2))
torch.rot90(my_tensor, k=-4, dims=(-1, 0))
torch.rot90(my_tensor, k=-4, dims=(-1, -2))
torch.rot90(my_tensor, k=-4, dims=(-2, 1))
torch.rot90(my_tensor, k=-4, dims=(-2, -1))
etc.
# tensor([[0, 1, 2], [3, 4, 5]])
torch.rot90(my_tensor, k=2)
torch.rot90(my_tensor, k=-2)
torch.rot90(my_tensor, k=2, dims=(0, 1))
torch.rot90(my_tensor, k=2, dims=(0, -1))
torch.rot90(my_tensor, k=2, dims=(1, 0))
torch.rot90(my_tensor, k=2, dims=(1, -2))
torch.rot90(my_tensor, k=2, dims=(-1, 0))
torch.rot90(my_tensor, k=2, dims=(-1, -2))
torch.rot90(my_tensor, k=2, dims=(-2, 1))
torch.rot90(my_tensor, k=2, dims=(-2, -1))
torch.rot90(my_tensor, k=-2, dims=(0, 1))
torch.rot90(my_tensor, k=-2, dims=(0, -1))
torch.rot90(my_tensor, k=-2, dims=(1, 0))
torch.rot90(my_tensor, k=-2, dims=(1, -2))
torch.rot90(my_tensor, k=-2, dims=(-1, 0))
torch.rot90(my_tensor, k=-2, dims=(-1, -2))
torch.rot90(my_tensor, k=-2, dims=(-2, 1))
torch.rot90(my_tensor, k=-2, dims=(-2, -1))
etc.
# tensor([[5, 4, 3], [2, 1, 0]])
torch.rot90(my_tensor, k=3)
torch.rot90(my_tensor, k=-1)
torch.rot90(my_tensor, dims=(1, 0))
torch.rot90(my_tensor, dims=(1, -2))
torch.rot90(my_tensor, k=1, dims=(1, 0))
torch.rot90(my_tensor, k=1, dims=(1, -2))
torch.rot90(my_tensor, k=1, dims=(-1, 0))
torch.rot90(my_tensor, k=1, dims=(-1, -2))
torch.rot90(my_tensor, k=3, dims=(0, 1))
torch.rot90(my_tensor, k=3, dims=(0, -1))
torch.rot90(my_tensor, k=3, dims=(-2, 1))
torch.rot90(my_tensor, k=3, dims=(-2, -1))
torch.rot90(my_tensor, k=-1, dims=(0, 1))
torch.rot90(my_tensor, k=-1, dims=(0, -1))
torch.rot90(my_tensor, k=-1, dims=(-2, 1))
torch.rot90(my_tensor, k=-1, dims=(-2, -1))
torch.rot90(my_tensor, k=-3, dims=(1, 0))
torch.rot90(my_tensor, k=-3, dims=(1, -2))
torch.rot90(my_tensor, k=-3, dims=(-1, 0))
torch.rot90(my_tensor, k=-3, dims=(-1, -2))
etc.
# tensor([[3, 0], [4, 1], [5, 2]])
my_tensor = torch.tensor([[False, True, 2.], [3., 4+0j, 5+0j]])
torch.rot90(my_tensor)
# tensor([[2.+0.j, 5.+0.j], [1.+0.j, 4.+0.j], [0.+0.j, 3.+0.j]])
roll() can shift the zero or more elements of a 0D or more D tensor to the right or left as shown below:
*Memos:
-
roll()
can be used withtorch
or a tensor. - The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
- The 2nd argument(
int
,tuple
ofint
orlist
ofint
) withtorch
or the 1st argument(int
,tuple
ofint
orlist
ofint
) with a tensor isshifts
(Required). - The 3rd argument(
int
,tuple
ofint
orlist
ofint
) withtorch
or the 2nd argument(int
,tuple
ofint
orlist
ofint
) with a tensor isdims
(Optional). - The number of
shifts
anddims
must be the same.
import torch
my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5]])
torch.roll(my_tensor, shifts=0)
my_tensor.roll(shifts=0)
torch.roll(my_tensor, shifts=-1)
torch.roll(my_tensor, shifts=0, dims=0)
torch.roll(my_tensor, shifts=0, dims=1)
torch.roll(my_tensor, shifts=0, dims=-1)
torch.roll(my_tensor, shifts=0, dims=-2)
torch.roll(my_tensor, shifts=2, dims=0)
torch.roll(my_tensor, shifts=2, dims=-2)
torch.roll(my_tensor, shifts=3, dims=1)
torch.roll(my_tensor, shifts=3, dims=-1)
torch.roll(my_tensor, shifts=4, dims=0)
torch.roll(my_tensor, shifts=4, dims=-2)
torch.roll(my_tensor, shifts=-2, dims=0)
torch.roll(my_tensor, shifts=-2, dims=-2)
torch.roll(my_tensor, shifts=-3, dims=1)
torch.roll(my_tensor, shifts=-3, dims=-1)
torch.roll(my_tensor, shifts=-4, dims=0)
torch.roll(my_tensor, shifts=-4, dims=-2)
torch.roll(my_tensor, shifts=0, dims=(0,))
torch.roll(my_tensor, shifts=0, dims=(1,))
torch.roll(my_tensor, shifts=0, dims=(-1,))
torch.roll(my_tensor, shifts=0, dims=(-2,))
torch.roll(my_tensor, shifts=2, dims=(0,))
torch.roll(my_tensor, shifts=3, dims=(1,))
torch.roll(my_tensor, shifts=3, dims=(-1,))
torch.roll(my_tensor, shifts=4, dims=(0,))
torch.roll(my_tensor, shifts=4, dims=(-2,))
torch.roll(my_tensor, shifts=-2, dims=(0,))
torch.roll(my_tensor, shifts=-2, dims=(-2,))
torch.roll(my_tensor, shifts=-3, dims=(1,))
torch.roll(my_tensor, shifts=-3, dims=(-1,))
torch.roll(my_tensor, shifts=-4, dims=(0,))
torch.roll(my_tensor, shifts=-4, dims=(-2,))
torch.roll(my_tensor, shifts=(0,), dims=0)
torch.roll(my_tensor, shifts=(0,), dims=1)
torch.roll(my_tensor, shifts=(0,), dims=-1)
torch.roll(my_tensor, shifts=(0,), dims=-2)
torch.roll(my_tensor, shifts=(2,), dims=0)
torch.roll(my_tensor, shifts=(2,), dims=-2)
torch.roll(my_tensor, shifts=(3,), dims=1)
torch.roll(my_tensor, shifts=(3,), dims=-1)
torch.roll(my_tensor, shifts=(4,), dims=0)
torch.roll(my_tensor, shifts=(4,), dims=-2)
torch.roll(my_tensor, shifts=(-2,), dims=0)
torch.roll(my_tensor, shifts=(-2,), dims=-2)
torch.roll(my_tensor, shifts=(-3,), dims=1)
torch.roll(my_tensor, shifts=(-3,), dims=-1)
torch.roll(my_tensor, shifts=(-4,), dims=0)
torch.roll(my_tensor, shifts=(-4,), dims=-2)
torch.roll(my_tensor, shifts=(0,), dims=(0,))
torch.roll(my_tensor, shifts=(0,), dims=(1,))
torch.roll(my_tensor, shifts=(0,), dims=(-1,))
torch.roll(my_tensor, shifts=(0,), dims=(-2,))
torch.roll(my_tensor, shifts=(2,), dims=(0,))
torch.roll(my_tensor, shifts=(2,), dims=(-2,))
torch.roll(my_tensor, shifts=(3,), dims=(1,))
torch.roll(my_tensor, shifts=(3,), dims=(-1,))
torch.roll(my_tensor, shifts=(4,), dims=(0,))
torch.roll(my_tensor, shifts=(4,), dims=(-2,))
torch.roll(my_tensor, shifts=(-2,), dims=(0,))
torch.roll(my_tensor, shifts=(-2,), dims=(-2,))
torch.roll(my_tensor, shifts=(-3,), dims=(1,))
torch.roll(my_tensor, shifts=(-3,), dims=(-1,))
torch.roll(my_tensor, shifts=(-4,), dims=(0,))
torch.roll(my_tensor, shifts=(-4,), dims=(-2,))
torch.roll(my_tensor, shifts=(0, 0), dims=(0, 0))
torch.roll(my_tensor, shifts=(0, 0), dims=(0, 1))
torch.roll(my_tensor, shifts=(0, 0), dims=(0, -1))
torch.roll(my_tensor, shifts=(0, 0), dims=(0, -2))
torch.roll(my_tensor, shifts=(0, 0), dims=(1, 0))
torch.roll(my_tensor, shifts=(0, 0), dims=(1, 1))
torch.roll(my_tensor, shifts=(0, 0), dims=(1, -1))
torch.roll(my_tensor, shifts=(0, 0), dims=(1, -2))
torch.roll(my_tensor, shifts=(0, 0), dims=(-1, 0))
torch.roll(my_tensor, shifts=(0, 0), dims=(-1, 1))
torch.roll(my_tensor, shifts=(0, 0), dims=(-1, -1))
torch.roll(my_tensor, shifts=(0, 0), dims=(-1, -2))
torch.roll(my_tensor, shifts=(0, 0), dims=(-2, 0))
torch.roll(my_tensor, shifts=(0, 0), dims=(-2, 1))
torch.roll(my_tensor, shifts=(0, 0), dims=(-2, -1))
torch.roll(my_tensor, shifts=(0, 0), dims=(-2, -2))
torch.roll(my_tensor, shifts=(0, -2), dims=(0, 0))
torch.roll(my_tensor, shifts=(0, -2), dims=(0, -2))
torch.roll(my_tensor, shifts=(0, -2), dims=(1, 0))
torch.roll(my_tensor, shifts=(0, -2), dims=(1, -2))
torch.roll(my_tensor, shifts=(0, -2), dims=(-1, 0))
torch.roll(my_tensor, shifts=(0, -2), dims=(-1, -2))
torch.roll(my_tensor, shifts=(0, -2), dims=(-2, 0))
torch.roll(my_tensor, shifts=(0, -2), dims=(-2, -2))
etc.
# tensor([[0, 1, 2], [3, 4, 5]])
torch.roll(my_tensor, shifts=1)
torch.roll(my_tensor, shifts=-5)
etc.
# tensor([[5, 0, 1], [2, 3, 4]])
torch.roll(my_tensor, shifts=2)
torch.roll(my_tensor, shifts=-4)
etc.
# tensor([[4, 5, 0], [1, 2, 3]])
torch.roll(my_tensor, shifts=3)
torch.roll(my_tensor, shifts=-3)
torch.roll(my_tensor, shifts=1, dims=0)
torch.roll(my_tensor, shifts=1, dims=-2)
torch.roll(my_tensor, shifts=3, dims=0)
torch.roll(my_tensor, shifts=3, dims=-2)
torch.roll(my_tensor, shifts=-1, dims=0)
torch.roll(my_tensor, shifts=-1, dims=-2)
torch.roll(my_tensor, shifts=-3, dims=0)
torch.roll(my_tensor, shifts=-3, dims=-2)
torch.roll(my_tensor, shifts=-5, dims=0)
torch.roll(my_tensor, shifts=-5, dims=-2)
torch.roll(my_tensor, shifts=1, dims=(0,))
torch.roll(my_tensor, shifts=1, dims=(-2,))
torch.roll(my_tensor, shifts=3, dims=(0,))
torch.roll(my_tensor, shifts=3, dims=(-2,))
torch.roll(my_tensor, shifts=-1, dims=(0,))
torch.roll(my_tensor, shifts=-1, dims=(-2,))
torch.roll(my_tensor, shifts=-3, dims=(0,))
torch.roll(my_tensor, shifts=-3, dims=(-2,))
torch.roll(my_tensor, shifts=-5, dims=(0,))
torch.roll(my_tensor, shifts=-5, dims=(-2,))
torch.roll(my_tensor, shifts=(1,), dims=0)
torch.roll(my_tensor, shifts=(1,), dims=-2)
torch.roll(my_tensor, shifts=(3,), dims=0)
torch.roll(my_tensor, shifts=(3,), dims=-2)
torch.roll(my_tensor, shifts=(5,), dims=0)
torch.roll(my_tensor, shifts=(5,), dims=-2)
torch.roll(my_tensor, shifts=(-1,), dims=0)
torch.roll(my_tensor, shifts=(-1,), dims=-2)
torch.roll(my_tensor, shifts=(-3,), dims=0)
torch.roll(my_tensor, shifts=(-3,), dims=-2)
torch.roll(my_tensor, shifts=(-5,), dims=0)
torch.roll(my_tensor, shifts=(-5,), dims=-2)
torch.roll(my_tensor, shifts=(1,), dims=(0,))
torch.roll(my_tensor, shifts=(1,), dims=(-2,))
torch.roll(my_tensor, shifts=(3,), dims=(0,))
torch.roll(my_tensor, shifts=(3,), dims=(-2,))
torch.roll(my_tensor, shifts=(5,), dims=(0,))
torch.roll(my_tensor, shifts=(5,), dims=(-2,))
torch.roll(my_tensor, shifts=(-1,), dims=(0,))
torch.roll(my_tensor, shifts=(-1,), dims=(-2,))
torch.roll(my_tensor, shifts=(-3,), dims=(0,))
torch.roll(my_tensor, shifts=(-3,), dims=(-2,))
torch.roll(my_tensor, shifts=(-5,), dims=(0,))
torch.roll(my_tensor, shifts=(-5,), dims=(-2,))
torch.roll(my_tensor, shifts=(0, 1), dims=(0, 0))
torch.roll(my_tensor, shifts=(0, 1), dims=(0, -2))
torch.roll(my_tensor, shifts=(0, 1), dims=(1, 0))
torch.roll(my_tensor, shifts=(0, 1), dims=(1, -2))
torch.roll(my_tensor, shifts=(0, 1), dims=(-1, 0))
torch.roll(my_tensor, shifts=(0, 1), dims=(-1, -2))
torch.roll(my_tensor, shifts=(0, 1), dims=(-2, 0))
torch.roll(my_tensor, shifts=(0, 1), dims=(-2, -2))
torch.roll(my_tensor, shifts=(0, -1), dims=(0, 0))
torch.roll(my_tensor, shifts=(0, -1), dims=(0, -2))
torch.roll(my_tensor, shifts=(0, -1), dims=(1, 0))
torch.roll(my_tensor, shifts=(0, -1), dims=(1, -2))
torch.roll(my_tensor, shifts=(0, -1), dims=(-1, 0))
torch.roll(my_tensor, shifts=(0, -1), dims=(-1, -2))
torch.roll(my_tensor, shifts=(0, -1), dims=(-2, 0))
torch.roll(my_tensor, shifts=(0, -1), dims=(-2, -2))
etc.
# tensor([[3, 4, 5], [0, 1, 2]])
torch.roll(my_tensor, shifts=4)
torch.roll(my_tensor, shifts=-2)
etc.
# tensor([[2, 3, 4], [5, 0, 1]])
torch.roll(my_tensor, shifts=1, dims=1)
torch.roll(my_tensor, shifts=1, dims=-1)
torch.roll(my_tensor, shifts=4, dims=1)
torch.roll(my_tensor, shifts=4, dims=-1)
torch.roll(my_tensor, shifts=-2, dims=1)
torch.roll(my_tensor, shifts=-2, dims=-1)
torch.roll(my_tensor, shifts=-5, dims=1)
torch.roll(my_tensor, shifts=-5, dims=-1)
torch.roll(my_tensor, shifts=1, dims=(1,))
torch.roll(my_tensor, shifts=1, dims=(-1,))
torch.roll(my_tensor, shifts=4, dims=(1,))
torch.roll(my_tensor, shifts=4, dims=(-1,))
torch.roll(my_tensor, shifts=-2, dims=(1,))
torch.roll(my_tensor, shifts=-2, dims=(-1,))
torch.roll(my_tensor, shifts=-5, dims=(1,))
torch.roll(my_tensor, shifts=-5, dims=(-1,))
torch.roll(my_tensor, shifts=(1,), dims=1)
torch.roll(my_tensor, shifts=(1,), dims=-1)
torch.roll(my_tensor, shifts=(4,), dims=1)
torch.roll(my_tensor, shifts=(4,), dims=-1)
torch.roll(my_tensor, shifts=(-2,), dims=1)
torch.roll(my_tensor, shifts=(-2,), dims=-1)
torch.roll(my_tensor, shifts=(-5,), dims=1)
torch.roll(my_tensor, shifts=(-5,), dims=-1)
torch.roll(my_tensor, shifts=(1,), dims=(1,))
torch.roll(my_tensor, shifts=(1,), dims=(-1,))
torch.roll(my_tensor, shifts=(4,), dims=(1,))
torch.roll(my_tensor, shifts=(4,), dims=(-1,))
torch.roll(my_tensor, shifts=(-2,), dims=(1,))
torch.roll(my_tensor, shifts=(-2,), dims=(-1,))
torch.roll(my_tensor, shifts=(-5,), dims=(1,))
torch.roll(my_tensor, shifts=(-5,), dims=(-1,))
torch.roll(my_tensor, shifts=(0, 1), dims=(0, 1))
torch.roll(my_tensor, shifts=(0, 1), dims=(0, -1))
torch.roll(my_tensor, shifts=(0, 1), dims=(1, 1))
torch.roll(my_tensor, shifts=(0, 1), dims=(1, -1))
torch.roll(my_tensor, shifts=(0, 1), dims=(-1, 1))
torch.roll(my_tensor, shifts=(0, 1), dims=(-1, -1))
torch.roll(my_tensor, shifts=(0, 1), dims=(-2, 1))
torch.roll(my_tensor, shifts=(0, 1), dims=(-2, -1))
torch.roll(my_tensor, shifts=(0, -2), dims=(0, 1))
torch.roll(my_tensor, shifts=(0, -2), dims=(0, -1))
torch.roll(my_tensor, shifts=(0, -2), dims=(1, 1))
torch.roll(my_tensor, shifts=(0, -2), dims=(1, -1))
torch.roll(my_tensor, shifts=(0, -2), dims=(-1, 1))
torch.roll(my_tensor, shifts=(0, -2), dims=(-1, -1))
torch.roll(my_tensor, shifts=(0, -2), dims=(-2, 1))
torch.roll(my_tensor, shifts=(0, -2), dims=(-2, -1))
etc.
# tensor([[2, 0, 1], [5, 3, 4]])
torch.roll(my_tensor, shifts=2, dims=1)
torch.roll(my_tensor, shifts=2, dims=-1)
torch.roll(my_tensor, shifts=-1, dims=1)
torch.roll(my_tensor, shifts=-1, dims=-1)
torch.roll(my_tensor, shifts=-4, dims=1)
torch.roll(my_tensor, shifts=-4, dims=-1)
torch.roll(my_tensor, shifts=2, dims=(1,))
torch.roll(my_tensor, shifts=2, dims=(-1,))
torch.roll(my_tensor, shifts=2, dims=(-2,))
torch.roll(my_tensor, shifts=-1, dims=(1,))
torch.roll(my_tensor, shifts=-1, dims=(-1,))
torch.roll(my_tensor, shifts=-4, dims=(1,))
torch.roll(my_tensor, shifts=-4, dims=(-1,))
torch.roll(my_tensor, shifts=(2,), dims=1)
torch.roll(my_tensor, shifts=(2,), dims=-1)
torch.roll(my_tensor, shifts=(5,), dims=1)
torch.roll(my_tensor, shifts=(5,), dims=-1)
torch.roll(my_tensor, shifts=(-1,), dims=1)
torch.roll(my_tensor, shifts=(-1,), dims=-1)
torch.roll(my_tensor, shifts=(-4,), dims=1)
torch.roll(my_tensor, shifts=(-4,), dims=-1)
torch.roll(my_tensor, shifts=(2,), dims=(1,))
torch.roll(my_tensor, shifts=(2,), dims=(-1,))
torch.roll(my_tensor, shifts=(5,), dims=(1,))
torch.roll(my_tensor, shifts=(5,), dims=(-1,))
torch.roll(my_tensor, shifts=(-1,), dims=(1,))
torch.roll(my_tensor, shifts=(-1,), dims=(-1,))
torch.roll(my_tensor, shifts=(-4,), dims=(1,))
torch.roll(my_tensor, shifts=(-4,), dims=(-1,))
torch.roll(my_tensor, shifts=(0, -1), dims=(0, 1))
torch.roll(my_tensor, shifts=(0, -1), dims=(0, -1))
torch.roll(my_tensor, shifts=(0, -1), dims=(1, -1))
torch.roll(my_tensor, shifts=(0, -1), dims=(-1, 1))
torch.roll(my_tensor, shifts=(0, -1), dims=(-1, -1))
torch.roll(my_tensor, shifts=(0, -1), dims=(-2, 1))
torch.roll(my_tensor, shifts=(0, -1), dims=(-2, -1))
etc.
# tensor([[1, 2, 0], [4, 5, 3]])
my_tensor = torch.tensor([[False, True, 2.], [3., 4+0j, 5+0j]])
torch.roll(my_tensor, shifts=0)
# tensor([[0.+0.j, 1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j, 5.+0.j]])
Top comments (0)