现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.flipud 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.flipud 是 PyTorch 中用于上下翻转(垂直翻转)张量的函数。

函数定义

torch.flipud(input)

使用示例

实例

import torch

# 二维张量上下翻转
x = torch.arange(12).reshape(3, 4)
print("原始张量:")
print(x)

result = torch.flipud(x)
print("上下翻转:")
print(result)

# 方阵上下翻转
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("n3x3 方阵:")
print(y)

result = torch.flipud(y)
print("上下翻转:")
print(result)

# 一维张量上下翻转
z = torch.tensor([1, 2, 3, 4])
result = torch.flipud(z)
print("n一维张量 [1, 2, 3, 4]:")
print("上下翻转:", result)

输出结果为:

原始张量:
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 参考手册