PyTorch torch.flipud function
PyTorch torch reference manual
torch.flipud A function in PyTorch for flipping a tensor upside down (vertically).
Function definition
torch.flipud(input)
Usage examples
Example
import
torch
Flip a 2D tensor upside down
x
=
torch.
arange
(
12
)
.
reshape
(
3
,
4
)
(
"Original tensor:"
)
(
x
)
result
=
torch.
flipud
(
x
)
(
"Flip upside down:"
)
(
result
)
Flip a square matrix upside down
y
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
)
(
"n3x3 square matrix:"
)
(
y
)
result
=
torch.
flipud
(
y
)
(
"Flip upside down:"
)
(
result
)
Flip a 1D tensor upside down
z
=
torch.
tensor
(
[
1
,
2
,
3
,
4
]
)
result
=
torch.
flipud
(
z
)
(
"n1D tensor [1, 2, 3, 4]:"
)
(
"Flip upside down:"
,
result
)
The output is:
原始张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Vertical flip:
tensor([[ 8, 9, 10, 11],
[ 4, 5, 6, 7],
[ 0, 1, 2, 3]])
3x3 方阵:
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Vertical flip:
tensor([[7, 8, 9],
[4, 5, 6],
[1, 2, 3]])
一维张量 [1, 2, 3, 4]:
上下翻转: tensor([4, 3, 2, 1])