PyTorch torch.fliplr Function
Pytorch torch Reference Manual
torch.fliplrIs a function in PyTorch used to flip a tensor left and right (horizontal flip).
Function Definition
torch.fliplr(input)
Usage Example
Example
import torch
# Flip a 2D tensor left and right
x = torch.arange(12).reshape(3, 4)
print("Original tensor:")
print(x)
result = torch.fliplr(x)
print("Left-right flip:")
print(result)
# Flip a square matrix left and right
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("n3x3 square matrix:")
print(y)
result = torch.fliplr(y)
print("Left-right flip:")
print(result)
# Vectors cannot be flipped; an error will be raised
z = torch.tensor([1, 2, 3])
try:
result = torch.fliplr(z)
except RuntimeError as e:
print(f"nError when flipping a 1D vector: {e}")
# Flip a 2D tensor left and right
x = torch.arange(12).reshape(3, 4)
print("Original tensor:")
print(x)
result = torch.fliplr(x)
print("Left-right flip:")
print(result)
# Flip a square matrix left and right
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("n3x3 square matrix:")
print(y)
result = torch.fliplr(y)
print("Left-right flip:")
print(result)
# Vectors cannot be flipped; an error will be raised
z = torch.tensor([1, 2, 3])
try:
result = torch.fliplr(z)
except RuntimeError as e:
print(f"nError when flipping a 1D vector: {e}")
The output result is:
原始张量:
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