Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.reshape

tf.reshape returns a tensor with a requested shape while preserving all values and their order. The total number of elements must stay unchanged; one shape dimension may be inferred with -1.

How it works

tf.reshape creates a new tensor view with the requested shape while preserving the input values and their order. The requested dimensions must contain the same total number of elements; one dimension may be -1 so TensorFlow can infer it, and [] converts a one-element tensor to a scalar.

Syntax

tf.reshape(tensor, shape, name=None)

Parameters

NameTypeDescription
tensorTensorInput Tensor to reshape.
shapeTensorOutput shape; must use int32 or int64 values.
namestringOptional name for the operation.

Returns

A Tensor with the same type as tensor.

Key points

  • Use tf.reshape to give a tensor a different shape.
  • The result preserves every input value and its sequence.
  • The shape parameter determines the result dimensions.
  • The operation is efficient because it can reuse the input data buffer.
  • Passing [-1] is a concise way to flatten a tensor.
  • The returned Tensor keeps the input tensor's data type.

Common mistakes

  • The new shape must preserve both the element count and value order; reshape is not an axis-reordering operation.
  • Only one output dimension may be inferred with -1.
  • An empty shape is valid only when the tensor contains exactly one element.

Example: Reshape a tensor

import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
y = tf.reshape(x, [3, 2])
print(y.numpy())
# [[1 2]
#  [3 4]
#  [5 6]]

Creates a two-dimensional tensor with the same six values in their original order.

When to use it

Prepare model inputs

Convert a flat batch or feature vector into the dimensions expected by a model layer.

Combine adjacent axes

Collapse several compatible dimensions into one while retaining row-major value order.

Flatten data

Turn a tensor into a single vector for downstream processing or inspection.

Create a scalar

Represent a one-element tensor as a scalar using an empty shape.

Interview questions

QWhat does tf.reshape do?

tf.reshape changes a tensor's shape without changing its values or their order. The output has the same element type as the input, and the total element count must remain constant.

Follow-up: How would you exchange two axes? Use tf.transpose; reshape itself does not rearrange axes.

QHow does -1 work in the shape argument?

The shape argument defines the output dimensions and may be an int32 or int64 tensor. One dimension can be -1, in which case TensorFlow computes it from the unchanged total size.

Follow-up: What does shape [-1] produce? It flattens the tensor into one dimension.

QWhat does tf.reshape return and why is it efficient?

tf.reshape returns a Tensor with the same type as tensor. It can reuse the underlying data buffer because it preserves element order and count, making the operation fast regardless of tensor size.

Follow-up: Does that mean no tensor object is created? No; the operation returns a new Tensor while potentially reusing the underlying storage.

  • tf.shape
  • tf.transpose
  • tf.expand_dims
  • tf.squeeze

Source: Reshape. Checked 2026-10-15.