PyTorch torch.fliplr function
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
)
(
"Original tensor:"
)
(
x
)
result
=
torch.
fliplr
(
x
)
(
"Left-right flip:"
)
(
result
)
Left-right flip of a square matrix
y
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
)
(
"n3x3 square matrix:"
)
(
y
)
result
=
torch.
fliplr
(
y
)
(
"Left-right flip:"
)
(
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:
(
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