Skip to main content

How to Transpose a Matrix in PyTorch with .T

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Transposing a matrix swaps its rows and columns, so a table of two rows and three columns becomes three rows of two. You need it whenever two matrices don't line up for multiplication. This guide transposes one with .T in PyTorch 2.15.0.

Syntax​

Syntax
tensor.T
PartWhat it means
tensorA tensor with two dimensions: a matrix
.TThe same values with rows and columns swapped. It's an attribute: no brackets after it

PyTorch's reference says .T "Returns a view of this tensor with its dimensions reversed." A view shares its values with the original tensor instead of copying them.

The Example​

A script makes a table of two rows and three columns, then prints it, its transpose, and the shapes of both.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

transpose.py
import torch

table = torch.tensor([[1, 2, 3], [4, 5, 6]])

print(table)
print(table.T)
print(table.shape, table.T.shape)

The transpose.py script in a macOS-style code window: a two-by-three tensor, then prints of it, of table.T, and of both shapes.

table.T is used twice, once to print the transposed values and once to read its shape.

Step 2: Run It​

Terminal
uv run python transpose.py
Output
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[1, 4],
[2, 5],
[3, 6]])
torch.Size([2, 3]) torch.Size([3, 2])

The terminal after uv run python transpose.py: the table 1, 2, 3 over 4, 5, 6, its transpose 1, 4 over 2, 5 over 3, 6, and the shapes torch.Size([2, 3]) and torch.Size([3, 2]).

The first row, 1, 2, 3, became the first column, and the shape went from [2, 3] to [3, 2]. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Changing the transpose and expecting the original to stay as it wasAfter flipped = table.T and flipped[0, 1] = 40, table printed 40 in place of its 4table.T.clone() makes a separate copy: with it, table stayed 1, 2, 3 over 4, 5, 6
.T on a tensor with three dimensionsUserWarning: The use of `x.T` on tensors of dimension other than 2 to reverse their shape is deprecated and it will throw an error in a future release..mT for a batch of matrices, as the FAQ shows
table.T() with bracketsTypeError: 'Tensor' object is not callabletable.T

FAQ​

How do I transpose a batch of matrices?​

With .mT, which "Returns a view of this tensor with the last two dimensions transposed". For a batch of two matrices of three rows and four columns, torch.zeros(2, 3, 4).mT.shape printed torch.Size([2, 4, 3]).

Why transpose before multiplying?​

Matrix multiplication needs the columns of the first matrix to match the rows of the second. Two tables of two rows and three columns don't fit: torch.ones(2, 3) @ torch.ones(2, 3) raised a RuntimeError. With the second one transposed, torch.ones(2, 3) @ torch.ones(2, 3).T printed a table of two rows and two columns. How to Multiply Matrices in PyTorch with torch.matmul explains the rule.

Conclusion​

table.T turned a table of two rows and three columns into three rows of two, torch.Size([3, 2]), sharing its values with the original. .clone() made an independent copy, and .mT transposes a batch of matrices.

Additional Resources​

  • torch.Tensor — PyTorch's reference for .T, .mT and the other tensor attributes.
  • Tensor views — which operations share values with the original tensor.
  • Tensors tutorial — PyTorch's introduction to tensors.