Skip to main content

How to Count the Elements of a Tensor in TensorFlow with tf.size

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

How many numbers does a tensor hold? For a photo, that's every pixel times every colour; for a table, every row times every column. tf.size counts them all. This guide counts the values of a small table in TensorFlow 2.22.0.

Syntax​

Syntax
tf.size(<tensor>, out_type=<data type>)
PartWhat it means
<tensor>The tensor whose values you want to count
out_type=<data type>Optional. The type of the count: tf.int32 unless you choose another, such as tf.int64

The tf.size API page: it "Returns a 0-D Tensor representing the number of elements in input of type out_type. Defaults to tf.int32." The tensor guide says what that number is: "The total number of items in the tensor, the product of the shape vector's elements."

The Example​

A table of scores: 2 players (rows) in 3 games (columns), 6 scores in all.

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​

sizes.py
import tensorflow as tf

scores = tf.constant([[7, 9, 8], [6, 10, 9]])

print(tf.size(scores))
print(tf.size(scores).numpy())
print(tf.size(scores, out_type=tf.int64))

The sizes.py script in a macOS-style code window: a scores tensor of two rows of three, then prints of tf.size(scores), tf.size(scores).numpy(), and tf.size(scores, out_type=tf.int64).

The script counts the scores three times: as a tensor, as a plain Python number with .numpy(), and as a tensor of type int64.

Step 2: Run It​

Terminal
uv run python sizes.py
Output
tf.Tensor(6, shape=(), dtype=int32)
6
tf.Tensor(6, shape=(), dtype=int64)

The terminal after uv run python sizes.py: a tensor holding 6 with dtype int32, the number 6, and a tensor holding 6 with dtype int64.

The table holds 6 scores: 2 rows × 3 columns. tf.size returned the count as a tensor with no axes, shape=(), of type int32. .numpy() turned it into the number 6, and out_type=tf.int64 changed only the type. 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
Counting with len(scores)It printed 2: len counts only along the first axis, the rowsUse tf.size(scores) for every value

FAQ​

When do I need out_type=tf.int64?​

When a tensor could hold more values than an int32 can count: more than 2,147,483,647. Below that, the default int32 is enough.

What's the size of a single number?​

1: tf.size(tf.constant(5)) returned 1. A single number has no axes, and the product of no lengths is 1.

Conclusion​

tf.size counted the 6 scores in a 2 × 3 table: the product of its shape's lengths. .numpy() gave the count as a Python number, out_type chose its type, and len only counted the rows.

Additional Resources​

  • tf.size — the count and its out_type.
  • About shapes — size, shape and rank in TensorFlow's tensor guide.