Change dimension order
Reordering tensor axes before a matrix or batched operation.
PyTorch · Function
Swaps two selected dimensions of a tensor and returns the transposed result. For strided tensors, the result shares underlying storage with the input.
torch.transpose returns a tensor with two selected dimensions exchanged. For strided tensors, the result is a view sharing the input's underlying storage; sparse tensors instead produce a result that does not share that storage.
torch.transpose(input, dim0, dim1)| Name | Type | Description |
|---|---|---|
input | Tensor | The tensor whose dimensions are exchanged. |
dim0 | int | The first dimension to transpose. |
dim1 | int | The second dimension to transpose. |
Tensor
import torch
x = torch.arange(6).reshape(2, 3)
y = torch.transpose(x, 0, 1)
print(x)
print(y)
# y has shape (3, 2), and shares storage with x.Swaps the first and second dimensions of a two-dimensional tensor.
Reordering tensor axes before a matrix or batched operation.
Converting row-major two-dimensional data into column-major-shaped data.
Rearranging batch and feature dimensions when preparing model inputs.
It exchanges the two dimensions identified by dim0 and dim1 and returns the transposed tensor. For a strided input, the result shares the input's underlying storage.
Follow-up: How would you avoid storage aliasing? Use torch.transpose_copy, which performs the same operation while creating fresh output tensors.
The input parameter is a Tensor, while dim0 and dim1 are integer dimension indices naming the pair to swap. The returned value is a Tensor.
Follow-up: What restriction applies to compressed sparse tensors? Both selected dimensions must belong to the batch dimensions or both must belong to the sparse dimensions.
A two-dimensional tensor with shape (2, 3) becomes shape (3, 2) when dimensions 0 and 1 are exchanged. More generally, only the selected dimension positions are swapped.
Follow-up: Which related operation is specialized for two-dimensional inputs? torch.t transposes dimensions 0 and 1, but expects an input with at most two dimensions.
torch.t()torch.Tensor.transposetorch.transpose_copytorch.swapaxestorch.swapdims