Skip to main content

How to Get the Shape of a Tensor in PyTorch

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Most errors in PyTorch code come down to a tensor having a different shape than you thought. Every tensor carries its shape with it, and PyTorch gives it to you two ways. This guide reads both, and the length of a single dimension, in PyTorch 2.15.0.

Syntax​

Syntax
tensor.shape
tensor.size(dim)
PartWhat it means
tensorAny tensor
.shapeThe length of every dimension, as a torch.Size, such as torch.Size([2, 3]) for two rows of three values. It's an attribute: no brackets after it
.size(dim)The same as .shape when called with nothing: .size(). With a dimension number dim, it gives that one length as a whole number

PyTorch's reference calls .shape an "Alias for size", and says torch.Size is "a subclass of tuple", so you can index it like one: tensor.shape[0] is the first dimension's length.

The Example​

A table of exam scores, two students by three subjects, as a tensor. The script prints its shape both ways, then the number of rows and of columns.

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​

tensor_shape.py
import torch

scores = torch.tensor([[90, 85, 77], [60, 72, 88]])

print(scores.shape)
print(scores.size())
print(scores.shape[0])
print(scores.shape[1])

The tensor_shape.py script in a macOS-style code window: a two-by-three tensor of scores, then print of scores.shape, scores.size(), scores.shape[0] and scores.shape[1].

torch.tensor builds the tensor from a list of two rows. Then the script asks for the shape as an attribute, as a method, and one dimension at a time.

Step 2: Run It​

Terminal
uv run python tensor_shape.py
Output
torch.Size([2, 3])
torch.Size([2, 3])
2
3

The terminal after uv run python tensor_shape.py: torch.Size([2, 3]) twice, then 2 and 3.

.shape and .size() both say torch.Size([2, 3]): two rows, three columns. Dimensions are counted from 0, so .shape[0] is the number of rows, 2, and .shape[1] the number of columns, 3. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
scores.shape() with bracketsTypeError: 'torch.Size' object is not callablescores.shape, or scores.size()
Counting dimensions from 1.shape[1] is the second dimension, the columnsStart at 0: .shape[0] is the rows
Using len(scores) for the number of valuesIt printed 2: len gives only the first dimensionscores.numel() counts every value: it printed 6

FAQ​

How many dimensions does a tensor have?​

scores.ndim printed 2 for the table: one dimension for the rows, one for the columns. len(scores.shape) gives the same 2.

How do I get the last dimension without knowing how many there are?​

Count from the end: scores.shape[-1] and scores.size(1) both printed 3 here, and -1 works whatever the number of dimensions.

Conclusion​

scores.shape and scores.size() both gave torch.Size([2, 3]) for a table of two rows and three columns, and scores.shape[0] and scores.shape[1] gave the two lengths one at a time.

Additional Resources​