Main Content

reshapeLayer

Reshape layer

Since R2025a

    Description

    A reshape layer reshapes data to a specified size.

    Creation

    Description

    layer = reshapeLayer(sz) creates a reshape layer that reshapes the layer input to the specified size.

    example

    layer = reshapeLayer(sz1,...,szN) creates a reshape layer that reshapes the layer input to have dimensions of the specified sizes.

    example

    layer = reshapeLayer(___,Name=Value) 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 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

    expand all

    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

    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 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

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

    The ReshapeLayer object stores this property as a character vector.

    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.

    size(Y)
    ans = 1×5
    
        14    56     1    12    15
    
    
    dims(Y)
    ans = 
    'SSCBT'
    

    Algorithms

    expand all

    Extended Capabilities

    expand all

    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