TensorFlow
tensor manipulation
placeholder
reshaping tensors
machine learning

Reshape tensor using placeholder value

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow, the placeholder value for reshape is -1. It does not mean any size is acceptable. It means TensorFlow may infer exactly one dimension from the total element count and the other dimensions you provide.

What -1 Actually Means in tf.reshape

tf.reshape changes only the shape metadata of a tensor. The number of elements must stay the same, so TensorFlow can infer one missing dimension only when the rest of the shape gives enough information.

python
1import tensorflow as tf
2
3x = tf.constant([1, 2, 3, 4, 5, 6])
4y = tf.reshape(x, [2, -1])
5
6print(y)
7print(y.shape)

The source tensor has six values. If the first dimension is fixed at 2, the second must be 3. The result is a 2 x 3 tensor.

This works the same way in eager mode and inside a tf.function. The rule is mathematical, not stylistic: TensorFlow divides the total number of elements by the product of the known dimensions.

Preserve the Element Count

A reshape is valid only if the target shape contains the same number of values as the source tensor.

python
1import tensorflow as tf
2
3x = tf.constant([1, 2, 3, 4, 5, 6])
4print(tf.reshape(x, [3, 2]))

That succeeds because 3 * 2 = 6. This one fails:

python
1import tensorflow as tf
2
3x = tf.constant([1, 2, 3, 4, 5, 6])
4# tf.reshape(x, [4, -1])

There is no integer value TensorFlow can infer for the second dimension because six elements cannot be split into four equal rows. When reshape fails, check the element count first. That is usually the real problem.

Only One Dimension Can Be Inferred

You may use -1 only once in a target shape.

python
1import tensorflow as tf
2
3x = tf.constant([1, 2, 3, 4, 5, 6])
4# tf.reshape(x, [-1, -1])

Two inferred dimensions would create an ambiguous shape. TensorFlow needs all but one dimension to be fully determined so it can solve the missing size.

A useful way to think about it is that -1 is not a wildcard. It is a single unknown in a solvable equation.

Dynamic Shapes and Batch Dimensions

The most common practical use is keeping the batch dimension dynamic while flattening the remaining axes.

python
1import tensorflow as tf
2
3x = tf.random.normal((8, 4, 4, 3))
4y = tf.reshape(x, [tf.shape(x)[0], -1])
5
6print(tf.shape(y))

Here the first dimension is taken from the runtime batch size, and the rest are flattened into one vector per example. This pattern is common before feeding image or sequence features into a dense layer.

If you write x.shape[0] instead, you are using static shape metadata. That can be None in traced functions, exported models, or layers that accept variable batch sizes. tf.shape(x)[0] is safer when the dimension must be read at runtime.

Reshape Does Not Reorder Data

Another common misconception is that reshape changes element order. It does not. TensorFlow reads the existing tensor in memory order and repackages it into a new shape.

python
1import tensorflow as tf
2
3x = tf.constant([[1, 2, 3], [4, 5, 6]])
4y = tf.reshape(x, [3, 2])
5
6print(y)

If you need a true reordering of axes, use tf.transpose, not tf.reshape.

python
1import tensorflow as tf
2
3x = tf.constant([[1, 2, 3], [4, 5, 6]])
4print(tf.transpose(x))

That distinction matters in model code. A reshape that looks plausible can silently produce the wrong layout if you really meant to swap axes.

Older Placeholder-Based Code

In TensorFlow 1 style code, people often asked about reshape with placeholders because shapes were less concrete until runtime. The reshape rule has not changed in TensorFlow 2. Whether the input comes from tf.placeholder in older code or from tensors in eager execution, -1 still means “infer one dimension from the rest”.

So if you are reading older examples, keep the API era separate from the reshape rule itself. The syntax around the tensor may differ, but the math is the same.

Common Pitfalls

  • Using more than one -1 in the target shape.
  • Forgetting that the total number of elements must remain unchanged.
  • Using x.shape when the dimension is only known at runtime.
  • Expecting reshape to transpose or reorder data.
  • Hardcoding batch sizes where tf.shape(x)[0] should be used.

Summary

  • In TensorFlow, -1 in tf.reshape means infer one dimension automatically.
  • TensorFlow can infer only one dimension per reshape call.
  • The source and target shapes must contain the same number of elements.
  • Use tf.shape(x)[0] when the batch dimension is dynamic at runtime.
  • 'tf.reshape changes shape, while tf.transpose changes axis order.'

Course illustration
Course illustration

All Rights Reserved.