PyTorch torch.rot90 function
PyTorch torch reference manual
torch.rot90 is a function in PyTorch used to rotate a tensor by 90 degrees.
Function definition
torch.rot90(input, k=1, dims=[0, 1])
Parameter Description:
input- Input tensork- number of rotations; positive means counterclockwise, negative means clockwise (default 1)dims- the plane dimensions in which to rotate (default [0, 1])
Usage examples
Example
import
torch
Rotate a two-dimensional tensor
x
=
torch.
arange
(
12
)
.
reshape
(
3
,
4
)
(
"Original tensor:"
)
(
x
)
result
=
torch.
rot90
(
x
,
1
)
(
"Rotate 90 degrees counterclockwise:"
)
(
result
)
result
=
torch.
rot90
(
x
,
1
)
(
"nRotate 90 degrees clockwise:"
)
(
result
)
result
=
torch.
rot90
(
x
,
2
)
(
"nRotate 180 degrees:"
)
(
result
)
Rotate along a specified plane (three-dimensional tensor)
y
=
torch.
arange
(
24
)
.
reshape
(
2
,
3
,
4
)
(
"nThree-dimensional tensor shape:"
,
y.
shape
)
result
=
torch.
rot90
(
y
,
1
,
dims
=
[
1
,
2
]
)
(
"Shape after rotating 90 degrees along the [1, 2] plane:"
,
result.
shape
)
The output is:
原始张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Rotate 90 degrees counter-clockwise:
tensor([[ 3, 7, 11],
[ 2, 6, 10],
[ 1, 5, 9],
[ 0, 4, 8]])
Rotate 90 degrees clockwise:
tensor([[ 8, 4, 0],
[ 9, 5, 1],
[10, 6, 2],
[11, 7, 3]])
Rotate 180 degrees:
tensor([[11, 10, 9, 8],
[ 7, 6, 5, 4],
[ 3, 2, 1, 0]])
三维张量形状: torch.Size([2, 3, 4])
沿 [1, 2] 平面旋转 90 度后形状: torch.Size([2, 4, 3])