Main Content

expandLayers

Expand network layers

Since R2024a

    Description

    netUpdated = expandLayers(net) expands all network layers in the dlnetwork object net, returning an equivalent network with no network layers. The software replaces each networkLayer with the layers it contains. The name of the network layer and a colon ":" delimiter are appended to the start of the names of the replacement layers. For example, the software replaces a network layer named "subnet" containing a fully-connected layer named "fc" with a fully-connected layer named "subnet:fc".

    example

    netUpdated = expandLayers(net,layerNames) expands only the network layers specified by name.

    netUpdated = expandLayers(net,layerIndices) expands only the network layers specified by index.

    example

    netUpdated = expandLayers(___,Name=Value) specifies options using one or more name-value arguments with any of the previous syntaxes. For example, specifying Delimiter="@" expands network layers and names the expanded layers "subnet@layerName" instead of "subnet:layerName".

    example

    Examples

    collapse all

    Create a 2-D residual neural network with an image input size of 100-by-100 pixel images and ten classes.

    resnet = resnetNetwork([100 100],10);

    Use the analyzeNetwork function to visualize the network. The network contains four stacks of residual blocks. The stacks contain three, four, six, and three residual blocks respectively.

    analyzeNetwork(resnet)

    Group the layers in the network and analyze the network. As the layer names in the residual blocks follow the naming pattern stackX:blockX:layerName, the groupLayers function groups each residual block into a different networkLayer object and each stack into a different networkLayer object, where each stack network layer contains several block network layers.

    resnet = groupLayers(resnet);
    analyzeNetwork(resnet)

    To undo the grouping, use the expandLayers function. Expand only the first level of network layers (i.e. the stacks but not the residual blocks) by specifying the recursive expansion name-value argument as false.

    resnet = expandLayers(resnet,Recursive=false);
    resnet.Layers
    ans = 
      24×1 Layer array with layers:
    
         1   'input'           Image Input                  100×100×1 images with 'zerocenter' normalization
         2   'conv1'           2-D Convolution              64 7×7×1 convolutions with stride [2  2] and padding 'same'
         3   'bn1'             Batch Normalization          Batch normalization with 64 channels
         4   'relu1'           ReLU                         ReLU
         5   'maxpool1'        2-D Max Pooling              3×3 max pooling with stride [2  2] and padding 'same'
         6   'stack1:block1'   Network Layer                Network with 12 layers, 2 inputs and 1 output.
         7   'stack1:block2'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
         8   'stack1:block3'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
         9   'stack2:block1'   Network Layer                Network with 12 layers, 2 inputs and 1 output.
        10   'stack2:block2'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        11   'stack2:block3'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        12   'stack2:block4'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        13   'stack3:block1'   Network Layer                Network with 12 layers, 2 inputs and 1 output.
        14   'stack3:block2'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        15   'stack3:block3'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        16   'stack3:block4'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        17   'stack3:block5'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        18   'stack3:block6'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        19   'stack4:block1'   Network Layer                Network with 12 layers, 2 inputs and 1 output.
        20   'stack4:block2'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        21   'stack4:block3'   Network Layer                Network with 10 layers, 2 inputs and 1 output.
        22   'gap'             2-D Global Average Pooling   2-D global average pooling
        23   'fc'              Fully Connected              10 fully connected layer
        24   'softmax'         Softmax                      softmax
    

    Expand all network layers in the network.

    resnet = expandLayers(resnet);

    Create an array of layers.

    layers = [sequenceInputLayer(6)
        fullyConnectedLayer(100)
        layerNormalizationLayer
        reluLayer
        fullyConnectedLayer(50)
        layerNormalizationLayer
        reluLayer
        softmaxLayer];

    Create a dlnetwork object. You can also create a dlnetwork object by training the network using the trainnet function.

    net = dlnetwork(layers);

    Group the layers with indices [2 3 4] and [5 6 7] into network layers, specifying the names of the grouped layers, and inspect the layers of the network.

    net = groupLayers(net,{[2 3 4] [5 6 7]},GroupNames=["fcBlock1" "fcBlock2"]);
    net.Layers
    ans = 
      4x1 Layer array with layers:
    
         1   'sequenceinput'   Sequence Input   Sequence input with 6 dimensions
         2   'fcBlock1'        Network Layer    Network with 3 layers, 1 input and 1 output.
         3   'fcBlock2'        Network Layer    Network with 3 layers, 1 input and 1 output.
         4   'softmax'         Softmax          softmax
    

    Expand network layer fcBlock1 using the expandLayers function, specifying the layer with index 2, and inspect the layers of the network.

    net = expandLayers(net,2);

    Inspect the layers of the network.

    net.Layers
    ans = 
      6x1 Layer array with layers:
    
         1   'sequenceinput'          Sequence Input        Sequence input with 6 dimensions
         2   'fcBlock1:fc_1'          Fully Connected       100 fully connected layer
         3   'fcBlock1:layernorm_1'   Layer Normalization   Layer normalization with 100 channels
         4   'fcBlock1:relu_1'        ReLU                  ReLU
         5   'fcBlock2'               Network Layer         Network with 3 layers, 1 input and 1 output.
         6   'softmax'                Softmax               softmax
    

    Create an array of layers, naming layers that you want to group into the same network layer with a group name followed by a delimiter, for example, an underscore.

    layers = [sequenceInputLayer(6,Name="input")
        fullyConnectedLayer(100,Name="group1_fc")
        layerNormalizationLayer(Name="group1_layerNorm")
        reluLayer(Name="group1_relu")
        fullyConnectedLayer(50,Name="group2_fc")
        layerNormalizationLayer(Name="group2_layerNorm")
        reluLayer(Name="group2_relu")
        softmaxLayer(Name="softmax")];

    Create a dlnetwork object. You can also create a dlnetwork object by training the network using the trainnet function.

    net = dlnetwork(layers);

    Group the layers in the network and inspect the layers of the network. The groupLayers function groups the layers with names starting with "group1_" into one networkLayer object and the layers with names starting with "group2_" into a second networkLayer object.

    net = groupLayers(net,Delimiter="_");
    net.Layers
    ans = 
      4x1 Layer array with layers:
    
         1   'input'     Sequence Input   Sequence input with 6 dimensions
         2   'group1'    Network Layer    Network with 3 layers, 1 input and 1 output.
         3   'group2'    Network Layer    Network with 3 layers, 1 input and 1 output.
         4   'softmax'   Softmax          softmax
    

    Expand the grouped layers using the expandLayers function and inspect the layers of the network. To ensure that the expanded layers have their original names, specify the delimiter as "_".

    net = expandLayers(net,Delimiter="_");
    net.Layers
    ans = 
      8x1 Layer array with layers:
    
         1   'input'              Sequence Input        Sequence input with 6 dimensions
         2   'group1_fc'          Fully Connected       100 fully connected layer
         3   'group1_layerNorm'   Layer Normalization   Layer normalization with 100 channels
         4   'group1_relu'        ReLU                  ReLU
         5   'group2_fc'          Fully Connected       50 fully connected layer
         6   'group2_layerNorm'   Layer Normalization   Layer normalization with 50 channels
         7   'group2_relu'        ReLU                  ReLU
         8   'softmax'            Softmax               softmax
    

    Input Arguments

    collapse all

    Neural network, specified as a dlnetwork object.

    The names of the network layers to expand, specified as a character vector, string scalar, or string array.

    Example: "subnet_1"

    Example: ["subnet_1" "subnet_2"]

    Data Types: char | string

    The indices of the network layers to expand, specified as an integer or vector of integers.

    Example: 4

    Example: [4 5 6]

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

    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: expandLayers(net,Recursive=false)

    The delimiter for naming replacement layers, specified as a string scalar or character vector.

    The name of the network layer and the delimiter are appended to the start to of the names of the replacement layers. For example, by default the software replaces a network layer named "subnet" containing a fully-connected layer named "fc" with a fully-connected layer named "subnet:fc".

    Data Types: char | string

    Flag for recursive expansion, specified as 1 (true) or 0 (false).

    • If Recursive is set to 1 (true), then for each network layer in net, including those nested within other network layers are expanded. For example, a network layer named "subnet" containing a network layer named "nestedNet" that contains a layer named "conv" will be replaced by a layer named "subnet:nestedNet:conv".

    • If Recursive is set to 0 (false), then network layers are expanded and any nested network layers are not affected.

    Data Types: logical

    Output Arguments

    collapse all

    Updated network, returned as a dlnetwork object.

    Version History

    Introduced in R2024a