reshapeLayer
Description
A reshape layer reshapes data to a specified size.
Creation
Description
creates a reshape layer that reshapes the layer input to the specified size.layer
= reshapeLayer(sz
)
creates a reshape layer that reshapes the layer input to have dimensions of the specified
sizes.layer
= reshapeLayer(sz1,...,szN
)
specifies options using one or more name-value arguments in addition to any of the input
argument combinations in previous syntaxes. For example,
layer
= reshapeLayer(___,Name=Value
)OperationDimension="spatial-channel"
specifies that the layer
reorders elements in the spatial and channel dimensions only.
Input Arguments
Output size, specified as a row vector of nonnegative integers. Each element of
sz
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
Specify optional pairs of arguments as
Name1=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:
"all"
— Reshape data by reordering the elements in all dimensions."spatial-channel"
— Reshape data by reordering the elements in the spatial and channel dimensions only. Use this option when you have image or image-sequence data with varying batch sizes or sequence lengths.
This argument sets the OperationDimension
property.
Data Types: char
| string
Properties
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 of
OutputSize
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:
"all"
— Reshape data by reordering the elements in all dimensions."spatial-channel"
— Reshape data by reordering the elements in the spatial and channel dimensions only.
Data Types: string
Layer
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
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.
size(Y)
ans = 1×5
14 56 1 12 15
dims(Y)
ans = 'SSCBT'
Algorithms
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:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
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
and
OperationDimension
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Version History
Introduced in R2025a
See Also
permuteLayer
| dlnetwork
| trainingOptions
| trainnet
| functionLayer
| crop2dLayer
| crop3dLayer
| complexToRealLayer
| realToComplexLayer
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)