Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.transpose

Swaps two selected dimensions of a tensor and returns the transposed result. For strided tensors, the result shares underlying storage with the input.

How it works

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.

Syntax

torch.transpose(input, dim0, dim1)

Parameters

NameTypeDescription
inputTensorThe tensor whose dimensions are exchanged.
dim0intThe first dimension to transpose.
dim1intThe second dimension to transpose.

Returns

Tensor

Key points

  • The function produces a transposed Tensor rather than modifying the input through an in-place API.
  • Only the two dimension positions supplied as dim0 and dim1 exchange places.
  • With strided tensors, the returned result is storage-aliasing and behaves like a view.
  • For sparse tensors, dimensions before the sparse dimensions are classified as batch dimensions.
  • torch.t is a related convenience operation for transposing low-dimensional inputs.

Common mistakes

  • For strided tensors, the result aliases the input storage, so changing values through either tensor can affect the other.
  • Sparse inputs have a non-aliasing result.
  • For compressed sparse layouts, do not exchange one batch dimension with one sparse dimension.
  • Swapping sparse dimensions can change the layout type for compressed sparse tensors.

Example: Swap two tensor dimensions

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.

When to use it

Change dimension order

Reordering tensor axes before a matrix or batched operation.

Transpose a matrix

Converting row-major two-dimensional data into column-major-shaped data.

Prepare model tensors

Rearranging batch and feature dimensions when preparing model inputs.

Interview questions

QWhat does torch.transpose do?

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.

QWhat are the parameters and return type?

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.

QHow does dimension swapping affect tensor shape?

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.transpose
  • torch.transpose_copy
  • torch.swapaxes
  • torch.swapdims

Source: torch.transpose — PyTorch main documentation. Checked 2026-10-16.