ReshapeLayer - Reshape layer - MATLAB (original) (raw)

Reshape layer

Since R2025a

Description

A reshape layer reshapes data to a specified size.

Creation

Syntax

Description

`layer` = reshapeLayer([sz](#mw%5Fe0693cce-b9e9-4b56-88a2-704c0d5be0b2)) creates a reshape layer that reshapes the layer input to the specified size.

example

`layer` = reshapeLayer([sz1,...,szN](#mw%5Fd9469eb6-26b0-4ca6-ba59-507082bbb40f)) creates a reshape layer that reshapes the layer input to have dimensions of the specified sizes.

example

`layer` = reshapeLayer(___,[Name=Value](#namevaluepairarguments)) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example,OperationDimension="spatial-channel" specifies that the layer reorders elements in the spatial and channel dimensions only.

example

Input Arguments

expand all

Output size, specified as a row vector of nonnegative integers. Each element ofsz indicates the size of the corresponding dimension in the layer output. You must specify sz so that the layer input and output have the same number of elements. That is, prod(sz) must be the same as the number of elements in the layer input.

This argument sets the OutputSize property.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension, specified as nonnegative integers or[]. You must specify at least 2 dimension sizes, and at most one dimension size can be [].

When a dimension size is [], the layer automatically calculates the size of that dimension to ensure that the number of elements in the layer input and output match. When you specify [], the dimensions that you explicitly specify must divide evenly into the number of elements in the layer input data.

To ensure that the layer can reshape the input for any batch size or sequence length, specify the variable dimension as [] so that the layer automatically calculates the size when it reshapes data. Alternatively, for image input, use the OperationDimension name-value argument.

These arguments set the OutputSize property.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: reshapeLayer([64 64 3],OperationDimension="spatial-channel") creates a reshape layer that reshapes the layer input so that it has size [l64 64 3] by reordering elements in the spatial and channel dimensions only.

Operation dimension, specified as one of these values:

This argument sets the OperationDimension property.

Data Types: char | string

Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet and dlnetwork functions automatically assign names to layers with the name "".

This argument sets the Name property.

Data Types: char | string

Properties

expand all

Reshape

This property is read-only after creation.

Output size, stored as a row vector of positive integers or a cell array of positive integers or []. Each element ofOutputSize indicates the size of the corresponding dimension in the layer output. If the element is [], then the layer automatically determines the corresponding dimension size when it reshapes the data.

When OperationDimension is "all" and the layer input data is a formatted dlarray object,numel(OutputSize) must be greater than or equal to the number of dimensions in the layer input. If numel(OutputSize) is greater than the number of dimensions of the layer input, then the layer introduces additional dimensions to the data with label "U" (unspecified). The layer removes trailing singleton dimensions with the label "U".

Data Types: double

This property is read-only after creation.

Operation dimension, stored as one of these values:

Data Types: string

Layer

Data Types: char | string

This property is read-only.

Number of inputs to the layer, stored as 1. This layer accepts a single input only.

Data Types: double

This property is read-only.

Input names, stored as {'in'}. This layer accepts a single input only.

Data Types: cell

This property is read-only.

Number of outputs from the layer, stored as 1. This layer has a single output only.

Data Types: double

This property is read-only.

Output names, stored as {'out'}. This layer has a single output only.

Data Types: cell

Examples

collapse all

Create a reshape layer that reshapes the layer input to 14-by-56-by-20-by-100 arrays.

layer = reshapeLayer([14 56 20 100])

layer = ReshapeLayer with properties:

              Name: ''

Hyperparameters OutputSize: [14 56 20 100] OperationDimension: "all"

Create a reshape layer that reshapes a batch of image data to 14-by-56-by-20-by-N arrays, where the layer automatically calculates the batch size N when it reshapes data.

layer = reshapeLayer(14,56,20,[])

layer = ReshapeLayer with properties:

              Name: ''

Hyperparameters OutputSize: {[14] [56] [20] []} OperationDimension: "all"

Include a reshape layer in a layer array.

layers = [ imageInputLayer([28 28 1]) convolution2dLayer(5,20,Padding="same") batchNormalizationLayer reshapeLayer(14,56,20,[]) reluLayer maxPooling2dLayer(2,Stride=2) fullyConnectedLayer(10) softmaxLayer];

Create a simple layer array for a network with image-sequence input that reshapes the spatial and channel dimensions only.

layers = [ sequenceInputLayer([28 28 1]) reshapeLayer([14 56 1],OperationDimension="spatial-channel")];

Create a batch of image-sequence data and pass it to the network. Specify that the data has a format of "SSCBT" (spatial, spatial, channel, batch, time).

X = rand([28 28 1 12 15]); X = dlarray(X,"SSCBT");

Create a neural network from the layer array and perform a forward pass with the data.

net = dlnetwork(layers); Y = forward(net,X);

View the size and format of the output data. Note that the layer reshapes the spatial and channel dimensions only.

Algorithms

expand all

Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray objects. The format of a dlarray object is a string of characters in which each character describes the corresponding dimension of the data. The format consists of one or more of these characters:

For example, you can describe 2-D image data that is represented as a 4-D array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, as having the format "SSCB" (spatial, spatial, channel, batch).

ReshapeLayer objects support input data of any format. The output format depends on the configuration of the OutputSize andOperationDimension properties.

ReshapeLayer objects support complex-valued input and outputs. The layer applies the same underlying operation to complex-valued input as it does to real-valued input. The layer outputs complex-valued data where applicable.

Extended Capabilities

Version History

Introduced in R2025a