Skip to main content

How to Get the Rank of a Tensor in TensorFlow

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor's rank is how many axes it has: 0 for a single number, 1 for a row, 2 for a table, 3 for a stack of tables, such as a colour photo. This guide reads the rank of four tensors with .ndim and tf.rank in TensorFlow 2.22.0.

Syntax​

Syntax
<tensor>.ndim
tf.rank(<tensor>)
PartWhat it means
<tensor>.ndimThe rank, as a Python int
tf.rank(<tensor>)The rank, as a tensor holding one int32

The tensor guide defines it: "Rank: Number of tensor axes. A scalar has rank 0, a vector has rank 1, a matrix is rank 2." The tf.rank API page adds a warning: "Note: The rank of a tensor is not the same as the rank of a matrix."

The Example​

Four tensors with 0, 1, 2 and 3 axes: a number, a row of three numbers, a table of two rows and a cube of two 2 × 2 tables.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

ranks.py
import tensorflow as tf

number = tf.constant(5)
row = tf.constant([1, 2, 3])
table = tf.constant([[1, 2, 3], [4, 5, 6]])
cube = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print("number:", number.ndim)
print("row: ", row.ndim)
print("table: ", table.ndim)
print("cube: ", cube.ndim)
print(tf.rank(cube))

The ranks.py script in a macOS-style code window: four tensors, a number, a row, a table and a cube, then prints of each one's ndim and of tf.rank(cube).

Count the opening square brackets at the start of each value: none for number, one for row, two for table, three for cube. That count is the rank.

Step 2: Run It​

Terminal
uv run python ranks.py
Output
number: 0
row: 1
table: 2
cube: 3
tf.Tensor(3, shape=(), dtype=int32)

The terminal after uv run python ranks.py: number 0, row 1, table 2, cube 3, then tf.rank(cube) as a tensor holding 3 with dtype int32.

The ranks are 0, 1, 2 and 3, one per level of brackets. tf.rank gave the cube's rank as a tensor holding 3. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Using ndim as a matrix's rank from linear algebraFor [[1, 2], [2, 4]], ndim was 2, because the table has two axes. Its second row is twice its first, so its rank in linear algebra is 1Use tf.linalg.matrix_rank, on decimal numbers: it returned 1 for [[1.0, 2.0], [2.0, 4.0]]

FAQ​

Is the rank the same as the number of values in the shape?​

Yes. The rank is how many lengths the shape lists: the table's shape (2, 3) has two, so its rank is 2. The tf.rank API page lists the other names it goes by: order, degree and ndims.

What rank does a colour photo have?​

3: its height, its width and its 3 colour channels. A batch of photos adds a fourth axis in front.

Conclusion​

.ndim gave 0, 1, 2 and 3 for a number, a row, a table and a cube, and tf.rank gave the same as a tensor. A tensor's rank counts its axes; a matrix's rank from linear algebra is something else, and tf.linalg.matrix_rank computes that one.

Additional Resources​