Pytorch Torch Fliplr
# PyTorch torch.fliplr Function
* * PyTorch torch Reference Manual](#)
`torch.fliplr` is a PyTorch function used to flip tensors left-to-right (horizontal flip).
### Function Definition
torch.fliplr(input)
* * *
## Usage Examples
## Example
import torch
# Flip 2D tensor left-right
x = torch.arange(12).reshape(3,4)
print("Original tensor:")
print(x)
result = torch.fliplr(x)
print("Left-right flipped:")
print(result)
# Flip square matrix
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 flipped:")
print(result)
# Vector flip will raise error
z = torch.tensor([1,2,3])
try:
result = torch.fliplr(z)
except RuntimeError as e:
print(f"n1D vector flip error: {e}")
Output results:
Original tensor: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])Left-right flipped: tensor([[ 3, 2, 1, 0], [ 7, 6, 5, 4], [11, 10, 9, 8]])3x3 Square matrix: tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])Left-right flipped: tensor([[3, 2, 1], [6, 5, 4], [9, 8, 7]])1D vector flip error: fliplr: Input must be at least 2-D
YouTip