Skip to main content

How to Reshape a Tensor in TensorFlow with tf.reshape

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The same twelve numbers can be a row of 12, a table of 3 rows of 4, or two tables of 2 × 3. tf.reshape changes how a tensor's values are arranged without changing the values or their order. This guide reshapes a row of 12 three ways in TensorFlow 2.22.0.

Syntax​

Syntax
tf.reshape(<tensor>, <new shape>)
PartWhat it means
<tensor>The tensor to rearrange
<new shape>A list with one length per axis, such as [3, 4]. Its lengths must multiply to the number of values. One of them may be -1, which means "work this one out"

The tf.reshape API page says what stays the same: "The tf.reshape does not change the order of or the total number of elements in the tensor". And what -1 does: "If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1."

The Example​

The numbers 1 to 12 in one row, reshaped into 3 rows of 4, into 2 rows of whatever length fits, and into two 2 × 3 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​

reshape_demo.py
import tensorflow as tf

numbers = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

print(tf.reshape(numbers, [3, 4]))
print(tf.reshape(numbers, [2, -1]))
print(tf.reshape(numbers, [2, 2, 3]).shape)

The reshape_demo.py script in a macOS-style code window: the numbers 1 to 12 in one tensor, reshaped to [3, 4], to [2, -1], and to [2, 2, 3], whose shape is printed.

12 values fit [3, 4] (3 × 4 = 12) and [2, 2, 3] (2 × 2 × 3 = 12). In [2, -1], TensorFlow works out the -1: 12 ÷ 2 = 6.

Step 2: Run It​

Terminal
uv run python reshape_demo.py
Output
tf.Tensor(
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]], shape=(2, 6), dtype=int32)
(2, 2, 3)

The terminal after uv run python reshape_demo.py: a 3 by 4 table of 1 to 12, a 2 by 6 table of 1 to 12, and the shape (2, 2, 3).

The values kept their order: 1 2 3 4 filled the first row, 5 6 7 8 the second, and so on. [2, -1] became (2, 6), with the 6 worked out. [2, 2, 3] gave two tables of 2 rows of 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
A shape the values don't fit: [5, -1] for 12 valuesInvalidArgumentError: … Input to reshape is a tensor with 12 values, but the requested shape requires a multiple of 5 [Op:Reshape]Use lengths that multiply to 12, or divide it, with -1
Two -1s: [-1, -1]InvalidArgumentError: Only one input size may be -1, not both 0 and 1 [Op:Reshape]Give every length but one
Using reshape to swap rows and columnsReshaping the 3 × 4 table to [4, 3] kept the values in order: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]. The columns didn't become rowsUse tf.transpose, which gave [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

FAQ​

How do I flatten a table back into one row?​

Reshape it to [-1]. The 3 × 4 table went back to [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].

Is reshaping slow for a big tensor?​

No. The API page says it "can reuse the underlying data buffer. This makes it a fast operation independent of how big of a tensor it is operating on."

Conclusion​

tf.reshape turned 12 numbers into a 3 × 4 table, a 2 × 6 table and two 2 × 3 tables, with the values always in the same order. -1 worked out one length, and a shape that the values don't fit stopped with an error saying how many values there are.

Additional Resources​