Feature-to-grid conversion
Convert a flat feature vector into a matrix or higher-rank tensor before subsequent layers.
Keras · Class
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.
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.
keras.layers.Reshape(target_shape, **kwargs)| Name | Type | Description |
|---|---|---|
target_shape | tuple of integers | Target shape for each sample, expressed as integer dimensions and excluding the batch dimension. |
**kwargs | keyword arguments | Additional layer configuration accepted by the base Layer class. |
A tensor whose shape is (batch_size, *target_shape).
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.
Convert a flat feature vector into a matrix or higher-rank tensor before subsequent layers.
Reorganize sequence or spatial data into dimensions expected by later computation.
Use -1 to preserve the element count while deriving one dimension automatically.
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.
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.
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.Flattenkeras.layers.RepeatVectorkeras.layers.UpSampling2D