MNIST
reshape operation
machine learning tutorial
deep learning
neural networks

Why the negative reshape -1 in MNIST tutorial?

Master System Design with Codemia

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

Introduction

In NumPy and TensorFlow reshaping code, -1 means “infer this dimension automatically from the data size and the other dimensions I specified.” In MNIST tutorials, it is commonly used so the code can flatten images or preserve batch size without hardcoding the number of examples.

What -1 Means in reshape

Suppose you have MNIST images stored as 28 x 28 arrays. If there are 60000 training images, the array shape is often (60000, 28, 28).

python
1import numpy as np
2
3x = np.zeros((60000, 28, 28), dtype=np.float32)
4print(x.shape)

If you want each image flattened to length 784, you can write:

python
flat = x.reshape(-1, 784)
print(flat.shape)

NumPy computes the missing first dimension automatically. Since 60000 * 28 * 28 total elements must still be present, the result becomes (60000, 784).

Why Tutorials Use It

The benefit is that the code does not need to repeat the dataset size explicitly.

python
flat = x.reshape(-1, 784)

is usually better than:

python
flat = x.reshape(60000, 784)

The first version still works if the batch or dataset size changes, as long as the total element count remains compatible.

Common MNIST Pattern: Preserve Batch Size

Inside model code, you often do not know the batch size in advance. That is why tutorials use patterns such as:

python
1import tensorflow as tf
2
3images = tf.zeros((32, 28, 28), dtype=tf.float32)
4flattened = tf.reshape(images, [-1, 784])
5print(flattened.shape)

Here, -1 means “keep however many images are in the batch, and flatten each one to 784 values.” For a batch of 32, the output becomes (32, 784).

Only One Dimension Can Be Inferred

You can use -1 only once in a reshape call because the missing size must be determined unambiguously.

python
1import numpy as np
2
3x = np.arange(12)
4print(x.reshape(3, -1))

This is valid. But something like reshape(-1, -1) is invalid because NumPy would not know how to compute both unknown dimensions.

The Total Number of Elements Must Still Match

reshape does not add or remove data. It only reinterprets the same elements under a new shape. So this fails:

python
1import numpy as np
2
3x = np.arange(10)
4# x.reshape(-1, 3)  # invalid because 10 is not divisible by 3

The inferred dimension must still produce the same total number of elements.

Why It Matters for Neural Networks

Fully connected layers expect vectors, not 2D image grids. MNIST tutorials therefore flatten each 28 x 28 image into a 784-element vector before feeding it into a dense layer.

The -1 part is about flexibility. The 784 part is about the per-image feature size.

When Not to Flatten

Not every MNIST model should reshape to (-1, 784). Convolutional networks usually keep the spatial structure and instead reshape to include a channel dimension.

python
1import tensorflow as tf
2
3images = tf.zeros((32, 28, 28), dtype=tf.float32)
4images = tf.reshape(images, [-1, 28, 28, 1])
5print(images.shape)

Here, -1 still preserves batch size, but the rest of the shape keeps the image layout for convolution layers.

Common Pitfalls

A common mistake is thinking -1 means “any shape is fine.” It does not. It means TensorFlow or NumPy should infer exactly one compatible dimension. Another is forgetting that flattening changes the data layout expected by later layers. Developers also sometimes hardcode dataset size instead of batch size, which makes the code less reusable and more brittle.

Summary

  • In reshape, -1 means infer this one dimension automatically.
  • MNIST tutorials often use it to preserve batch size while flattening images.
  • 'reshape(-1, 784) turns 28 x 28 images into feature vectors of length 784.'
  • Only one dimension can be inferred in a single reshape call.
  • The total number of elements must remain unchanged after reshaping.

Course illustration
Course illustration

All Rights Reserved.