PyTorch torch.fliplr Funktion


Pytorch torch 参考手册PyTorch torch Referenzhandbuch

torch.fliplrIst eine Funktion in PyTorch, mit der ein Tensor nach links und rechts gespiegelt wird (horizontale Spiegelung).

Funktionsdefinition

torch.fliplr(input)

Anwendungsbeispiel

Beispiel

import torch

# Einen 2D-Tensor links und rechts spiegeln
x = torch.arange(12).reshape(3, 4)
print("Original-Tensor:")
print(x)

result = torch.fliplr(x)
print("Links-Rechts-Spiegelung:")
print(result)

# Eine quadratische Matrix links und rechts spiegeln
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("n3x3 quadratische Matrix:")
print(y)

result = torch.fliplr(y)
print("Links-Rechts-Spiegelung:")
print(result)

# Vektoren können nicht gespiegelt werden; ein Fehler wird ausgelöst
z = torch.tensor([1, 2, 3])
try:
    result = torch.fliplr(z)
except RuntimeError as e:
    print(f"nFehler beim Spiegeln eines 1D-Vektors: {e}")

Das Ausgabeergebnis ist:

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

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

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