How to Get the Shape of a Tensor in TensorFlow
A tensor's shape tells you how many values it has along each side: how many rows, how many columns, and so on. It's the first thing to check when tensors don't fit together. This guide reads the shape of a small table in four ways, in TensorFlow 2.22.0.
Syntax
<tensor>.shape
<tensor>.shape[<axis>]
<tensor>.shape.as_list()
tf.shape(<tensor>)
| Part | What it means |
|---|---|
<tensor>.shape | The shape, as a TensorShape: one length per side, which TensorFlow calls an axis |
[<axis>] | The length of one axis, counted from 0: [0] is the rows of a table, [1] its columns |
.as_list() | The same lengths as an ordinary Python list |
tf.shape(<tensor>) | The shape as a tensor of whole numbers |
The tensor guide defines a shape as "The length (number of elements) of each of the axes of a tensor." The tf.shape API page says when the two forms differ: "tf.shape and Tensor.shape should be identical in eager mode. Within tf.function or within a compat.v1 context, not all dimensions may be known until execution time."
The Example
A table of scores: 2 players (rows) in 3 games (columns). The script reads its shape every way the syntax shows.
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
import tensorflow as tf
scores = tf.constant([[7, 9, 8], [6, 10, 9]])
print(scores.shape)
print(scores.shape[0], "rows and", scores.shape[1], "columns")
print(scores.shape.as_list())
print(tf.shape(scores))
![The shapes.py script in a macOS-style code window: a scores tensor with two rows of three numbers, then prints of scores.shape, shape[0] rows and shape[1] columns, shape.as_list(), and tf.shape(scores).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/tensorflow/2026/tensorflow-shape-01-code.webp)
scores has two rows, one per player, with three scores in each. The script then asks for its shape as a whole, one axis at a time, as a list and as a tensor.
Step 2: Run It
uv run python shapes.py
(2, 3)
2 rows and 3 columns
[2, 3]
tf.Tensor([2 3], shape=(2,), dtype=int32)
![The terminal after uv run python shapes.py: (2, 3), then 2 rows and 3 columns, then the list [2, 3], then a tensor [2 3] with shape (2,) and dtype int32.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/tensorflow/2026/tensorflow-shape-02-output.webp)
(2, 3) reads "2 along the first axis, 3 along the second": 2 rows of 3. shape[0] and shape[1] picked those lengths out, and as_list() gave [2, 3] as a plain list. tf.shape returned the same numbers as a tensor, which code inside TensorFlow's graphs can compute with. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling the shape like a function: scores.shape() | TypeError: 'TensorShape' object is not callable. shape is a property, not a method | Write scores.shape, without brackets |
Reading shape[0] as the number of values | It's only the length of the first axis: 2 here, while the table holds 6 values | Multiply the lengths, or use tf.size |
FAQ
What's the shape of a single number?
(), an empty shape: a single number has no axes. A row of 4 numbers has the shape (4,), where the comma shows it's a one-item list of lengths.
When should I use tf.shape instead of .shape?
Inside a tf.function or a model, where some lengths, such as the number of photos in a batch, aren't known until the code runs. There the API page says to "prefer the dynamic tf.shape(x) over the static x.shape". In ordinary code, as here, both give the same numbers.
Conclusion
scores.shape was (2, 3): 2 rows and 3 columns. Indexing it picked out one length, as_list() made it a Python list, and tf.shape returned it as a tensor. And shape is a property, so it never takes brackets.
Additional Resources
- About shapes — shape, rank, axis and size in TensorFlow's tensor guide.
- tf.shape — the shape as a tensor.
- tf.TensorShape — what
.shapereturns.
