PyTorch torch.flipud Function


Pytorch torch 参考手册PyTorch torch Reference Manual

torch.flipudA function in PyTorch used to flip a tensor upside down (vertically flip).

Function Definition

torch.flipud(input)

Usage Examples

Example

import torch

# Flip a 2D tensor upside down
x = torch.arange(12).reshape(3, 4)
print("Original tensor:")
print(x)

result = torch.flipud(x)
print("Flipped upside down:")
print(result)

# Flip a square matrix upside down
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("3x3 square matrix:")
print(y)

result = torch.flipud(y)
print("Flipped upside down:")
print(result)

# Flip a 1D tensor upside down
z = torch.tensor([1, 2, 3, 4])
result = torch.flipud(z)
print("1D tensor [1, 2, 3, 4]:")
print("Flipped upside down:", result)

The output is:

原始张量:
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
上下翻转:
tensor([[ 8,  9, 10, 11],
        [ 4,  5,  6,  7],
        [ 0,  1,  2,  3]])

3x3 方阵:
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
上下翻转:
tensor([[7, 8, 9],
        [4, 5, 6],
        [1, 2, 3]])

一维张量 [1, 2, 3, 4]:
上下翻转: tensor([4, 3, 2, 1])

Pytorch torch 参考手册PyTorch torch Reference Manual

Other Extensions