YouTip LogoYouTip

Pytorch Torch Rot90

## PyTorch torch.rot90 The `torch.rot90` function in PyTorch is used to rotate a tensor by 90 degrees in the plane specified by its dimensions. This operation is highly useful in computer vision tasks, such as data augmentation (e.g., rotating images or feature maps) and spatial transformations. --- ### Function Definition ```python torch.rot90(input, k=1, dims=[0, 1]) -> Tensor ``` ### Parameter Descriptions * **`input`** (*Tensor*): The input tensor to be rotated. * **`k`** (*int, optional*): The number of times the tensor is rotated by 90 degrees. * A **positive** value indicates counter-clockwise rotation. * A **negative** value indicates clockwise rotation. * Default: `1` (a single 90-degree counter-clockwise rotation). * **`dims`** (*list* or *tuple of ints, optional*): The plane of rotation defined by two dimensions. The rotation is performed in the plane of these two axes. * Default: `[0, 1]`. --- ## Code Examples ### 1. Rotating a 2D Tensor (Matrix) The following example demonstrates how to rotate a 2D tensor counter-clockwise, clockwise, and by 180 degrees. ```python import torch # Create a 2D tensor of shape (3, 4) x = torch.arange(12).reshape(3, 4) print("Original Tensor:") print(x) print("-" * 30) # Rotate 90 degrees counter-clockwise (k=1) result_ccw = torch.rot90(x, 1) print("Rotated 90 degrees Counter-Clockwise (k=1):") print(result_ccw) print("-" * 30) # Rotate 90 degrees clockwise (k=-1) result_cw = torch.rot90(x, -1) print("Rotated 90 degrees Clockwise (k=-1):") print(result_cw) print("-" * 30) # Rotate 180 degrees (k=2) result_180 = torch.rot90(x, 2) print("Rotated 180 degrees (k=2):") print(result_180) ``` **Output:** ```text Original Tensor: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) ------------------------------ Rotated 90 degrees Counter-Clockwise (k=1): tensor([[ 3, 7, 11], [ 2, 6, 10], [ 1, 5, 9], [ 0, 4, 8]]) ------------------------------ Rotated 90 degrees Clockwise (k=-1): tensor([[ 8, 4, 0], [ 9, 5, 1], [10, 6, 2], [11, 7, 3]]) ------------------------------ Rotated 180 degrees (k=2): tensor([[11, 10, 9, 8], [ 7, 6, 5, 4], [ 3, 2, 1, 0]]) ``` --- ### 2. Rotating a 3D Tensor Along a Specific Plane When working with multi-dimensional tensors (such as image batches with shape `[Channels, Height, Width]`), you can specify which dimensions to rotate using the `dims` parameter. ```python import torch # Create a 3D tensor of shape (2, 3, 4) y = torch.arange(24).reshape(2, 3, 4) print("Original 3D Tensor Shape:", y.shape) # Rotate 90 degrees counter-clockwise along the plane of the last two dimensions [1, 2] # This corresponds to rotating the spatial dimensions (Height and Width) of an image result = torch.rot90(y, 1, dims=[1, 2]) print("Shape after rotating 90 degrees along plane [1, 2]:", result.shape) ``` **Output:** ```text Original 3D Tensor Shape: torch.Size([2, 3, 4]) Shape after rotating 90 degrees along plane [1, 2]: torch.Size([2, 4, 3]) ``` --- ## Important Considerations 1. **Memory View**: `torch.rot90` returns a **new tensor** with its own data copy, rather than a view of the original tensor. This is because rotation changes the memory layout in a way that cannot always be represented by strides alone. 2. **Dimension Constraints**: The `dims` parameter must contain exactly two distinct dimensions. These dimensions must be within the valid range of the input tensor's dimensions (i.e., between `-input.ndim` and `input.ndim - 1`). 3. **Equivalence of `k`**: Since rotation is periodic every 360 degrees, `k=4` returns the tensor to its original state. Thus, `k=1` is equivalent to `k=-3`, and `k=2` is equivalent to `k=-2`.
← Pytorch Torch Row_StackPytorch Torch Result_Type β†’