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

Keras · Class

layers.Reshape

keras.layers.Reshape changes each input sample to a specified shape while preserving the batch axis. One target dimension may be -1 for automatic size inference.

How it works

Reshape changes the non-batch dimensions of each input example to target_shape without changing the data values. The target must be compatible with the input size; one target dimension may be -1 so Keras can infer it from the remaining dimensions.

Syntax

keras.layers.Reshape(target_shape, **kwargs)

Parameters

NameTypeDescription
target_shapetuple of integersTarget shape for each sample, expressed as integer dimensions and excluding the batch dimension.
**kwargskeyword argumentsAdditional layer configuration accepted by the base Layer class.

Returns

A tensor whose shape is (batch_size, *target_shape).

Key points

  • Reshape changes the arrangement of each input example into the requested shape.
  • target_shape contains integer feature dimensions only; the batch dimension is supplied separately.
  • Inputs may have different ranks, but their per-example sizes must be compatible with the target.
  • The resulting tensor keeps the batch size and appends the requested target dimensions.
  • A single -1 can stand for an inferred target dimension.

Common mistakes

  • Only one target dimension may be -1; Keras infers that dimension from the input size and the other target dimensions.

Example: Reshape a feature vector

import keras

x = keras.Input(shape=(12,))
y = keras.layers.Reshape((3, 4))(x)
model = keras.Model(x, y)
print(model.output_shape)  # (None, 3, 4)

Creates a model that converts each 12-element input into a 3-by-4 tensor.

When to use it

Feature-to-grid conversion

Convert a flat feature vector into a matrix or higher-rank tensor before subsequent layers.

Tensor layout preparation

Reorganize sequence or spatial data into dimensions expected by later computation.

Shape inference

Use -1 to preserve the element count while deriving one dimension automatically.

Interview questions

QWhat does Keras Reshape do to the input and batch dimension?

It reshapes each example to the requested target_shape while keeping the batch axis separate. For example, an input with 12 features can become shape (3, 4), producing an output shape of (batch_size, 3, 4).

Follow-up: What must be true for the reshape to work? The input dimensions must be compatible with target_shape, meaning the per-example element counts must agree.

QHow is target_shape specified?

target_shape is a tuple of integers describing the output dimensions for one example; it does not include the batch size. The output shape is therefore (batch_size, *target_shape).

Follow-up: How can a variable target dimension be represented? Use one -1, which Keras infers from the array size and the remaining dimensions.

QWhat constraints apply to the input and inferred dimensions?

The input can have an arbitrary rank, provided its dimensions are compatible with the requested target. A target dimension of -1 lets Keras infer one missing size.

Follow-up: Can multiple dimensions be inferred? No; the documented form permits one target element to be -1.

  • keras.layers.Flatten
  • keras.layers.RepeatVector
  • keras.layers.UpSampling2D

Source: Reshape layer. Checked 2026-10-15.