Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.rot90 function

PyTorch torch.rot90 function


PyTorch torch reference manual 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:


Usage examples

Example

import

torch

Rotate a two-dimensional tensor

x

=

torch.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

"Original tensor:"

)

print

(

x

)

result

=

torch.

rot90

(

x

,

1

)

print

(

"Rotate 90 degrees counterclockwise:"

)

print

(

result

)

result

=

torch.

rot90

(

x

,

1

)

print

(

"nRotate 90 degrees clockwise:"

)

print

(

result

)

result

=

torch.

rot90

(

x

,

2

)

print

(

"nRotate 180 degrees:"

)

print

(

result

)

Rotate along a specified plane (three-dimensional tensor)

y

=

torch.

arange

(

24

)

.

reshape

(

2

,

3

,

4

)

print

(

"nThree-dimensional tensor shape:"

,

y.

shape

)

result

=

torch.

rot90

(

y

,

1

,

dims

=

[

1

,

2

]

)

print

(

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


PyTorch torch reference manual PyTorch torch reference manual