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

PyTorch torch.fliplr function

PyTorch torch.fliplr function


PyTorch torch reference manual PyTorch torch reference manual

torch.fliplr is a PyTorch function for flipping a tensor left-to-right (horizontal flip).

Function definition

torch.fliplr(input)


Usage examples

Example

import

torch

Left-right flip of a 2D tensor

x

=

torch.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

"Original tensor:"

)

print

(

x

)

result

=

torch.

fliplr

(

x

)

print

(

"Left-right flip:"

)

print

(

result

)

Left-right flip of a square matrix

y

=

torch.

tensor

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

,

[

7

,

8

,

9

]

]

)

print

(

"n3x3 square matrix:"

)

print

(

y

)

result

=

torch.

fliplr

(

y

)

print

(

"Left-right flip:"

)

print

(

result

)

A vector cannot be flipped and will raise an error

z

=

torch.

tensor

(

[

1

,

2

,

3

]

)

try

:

    result

=

torch.

fliplr

(

z

)

except

RuntimeError

as

e:

   

print

(

f

"n1D vector flip error: {e}"

)

The output is:

原始张量:

tensor([[ 0,  1,  2,  3],

        [ 4,  5,  6,  7],

        [ 8,  9, 10, 11]])

Horizontal flip:

tensor([[ 3,  2,  1,  0],

        [ 7,  6,  5,  4],

        [11, 10,  9,  8]])



3x3 方阵:

tensor([[1, 2, 3],

        [4, 5, 6],

        [7, 8, 9]])

Horizontal flip:

tensor([[3, 2, 1],

        [6, 5, 4],

        [9, 8, 7]])



一维向量翻转报错: fliplr: Input must be at least 2-D