PyTorch torch.fliplr 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

torch.fliplrPyTorch でテンソルを左右反転(水平反転)するための関数です。

関数定義

torch.fliplr(input)

使用例

例

import torch

# 2次元テンソルを左右反転
x = torch.arange(12).reshape(3, 4)
print("元のテンソル:")
print(x)

result = torch.fliplr(x)
print("左右反転:")
print(result)

# 正方行列を左右反転
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("n3x3 正方行列:")
print(y)

result = torch.fliplr(y)
print("左右反転:")
print(result)

# ベクトルは反転できないため、エラーになります
z = torch.tensor([1, 2, 3])
try:
    result = torch.fliplr(z)
except RuntimeError as e:
    print(f"n1次元ベクトルの反転エラー: {e}")

出力結果は次のとおりです:

原始张量:
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