0
0
PytorchHow-ToBeginner · 3 min read

How to Transpose Tensor in PyTorch: Syntax and Examples

In PyTorch, you can transpose a tensor using tensor.t() for 2D tensors or tensor.transpose(dim0, dim1) for tensors with any number of dimensions. The transpose method swaps the specified dimensions, while t() is a shortcut for swapping the last two dimensions of 2D tensors.
📐

Syntax

1. tensor.t(): Transposes a 2D tensor by swapping its rows and columns.

2. tensor.transpose(dim0, dim1): Swaps two specified dimensions dim0 and dim1 of a tensor with any number of dimensions.

python
transposed_tensor = tensor.t()
transposed_tensor = tensor.transpose(dim0, dim1)
💻

Example

This example shows how to transpose a 2D tensor using t() and how to swap dimensions in a 3D tensor using transpose().

python
import torch

# Create a 2D tensor
tensor_2d = torch.tensor([[1, 2, 3], [4, 5, 6]])
print("Original 2D tensor:")
print(tensor_2d)

# Transpose using t()
transposed_2d = tensor_2d.t()
print("\nTransposed 2D tensor using t():")
print(transposed_2d)

# Create a 3D tensor
tensor_3d = torch.arange(24).reshape(2, 3, 4)
print("\nOriginal 3D tensor shape:", tensor_3d.shape)

# Transpose dimensions 1 and 2
transposed_3d = tensor_3d.transpose(1, 2)
print("Transposed 3D tensor shape (swap dim 1 and 2):", transposed_3d.shape)
print(transposed_3d)
Output
Original 2D tensor: tensor([[1, 2, 3], [4, 5, 6]]) Transposed 2D tensor using t(): tensor([[1, 4], [2, 5], [3, 6]]) Original 3D tensor shape: torch.Size([2, 3, 4]) Transposed 3D tensor shape (swap dim 1 and 2): torch.Size([2, 4, 3]) tensor([[[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]], [[12, 16, 20], [13, 17, 21], [14, 18, 22], [15, 19, 23]]])
⚠️

Common Pitfalls

  • Using t() on tensors with more than 2 dimensions will raise an error because t() only works on 2D tensors.
  • Confusing transpose() with permute(): transpose() swaps two dimensions, while permute() rearranges all dimensions.
  • Not specifying correct dimension indices in transpose() can lead to unexpected shapes.
python
import torch

# Wrong: Using t() on 3D tensor
try:
    tensor_3d = torch.randn(2, 3, 4)
    tensor_3d.t()
except RuntimeError as e:
    print(f"Error using t() on 3D tensor: {e}")

# Right: Use transpose() to swap dims
transposed = tensor_3d.transpose(1, 2)
print("Correct transpose shape:", transposed.shape)
Output
Error using t() on 3D tensor: t() expects a 2D tensor, but self is 3D Correct transpose shape: torch.Size([2, 4, 3])
📊

Quick Reference

Summary:

  • tensor.t(): Transpose 2D tensor (swap rows and columns).
  • tensor.transpose(dim0, dim1): Swap any two dimensions of a tensor.
  • Use permute() to reorder multiple dimensions.

Key Takeaways

Use tensor.t() only for 2D tensors to swap rows and columns.
Use tensor.transpose(dim0, dim1) to swap any two dimensions in tensors of any shape.
Do not use t() on tensors with more than 2 dimensions; it will cause an error.
transpose() swaps exactly two dimensions, while permute() can reorder all dimensions.
Always check tensor shapes before and after transposing to avoid shape mismatches.