Main Content

groupLayers

Group layers into network layers

Since R2024a

    Description

    groupLayers groups layers in a network whose names contain a specific pattern or groups layers specified by their names or indices.

    Group Layers by Pattern

    example

    netUpdated = groupLayers(net) groups the layers in the dlnetwork object net whose names start with any text followed by a colon into networkLayer objects. Layers whose names start with the same pattern are grouped into the same network layer. For example, the software groups layers named "subnet:fc" and "subnet:relu" into a network layer named "subnet".

    Use this syntax to group the layers previously expanded by the expandLayers function.

    example

    netUpdated = groupLayers(net,Delimiter=delim) specifies the delimiter for grouping by pattern. For example, specifying Delimiter="@" groups layers whose names contain the pattern "subnet@" instead of "subnet:".

    netUpdated = groupLayers(___,Recursive=tf) specifies whether to recursively group layers for each instance of the pattern in a layer name or only for the first instance using any of the previous syntaxes.

    Group Layers by Names or Indices

    example

    netUpdated = groupLayers(net,groups) groups layers in net specified by groups into networkLayer objects.

    example

    netUpdated = groupLayers(net,groups,GroupNames=Names) groups layers in net specified by groups into networkLayer objects. The names of the network layers are specified by Names.

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

    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
    

    Input Arguments

    collapse all

    Neural network, specified as a dlnetwork object.

    The layers to group into network layers. To group a single set of layers, specify groups as one of the following:

    • An array of layer indices, for example [4 5 6].

    • An array of layer names, for example ["conv" "layernorm" "relu"].

    To group multiple sets of layers into separate network layers, specify groups as:

    • A cell array containing arrays of layer indices or arrays of layer names, for example {[4 5 6] [9 10 11]}. Each cell in the cell array defines a set of layers that are grouped into a single network layer. The cell array can contain both layer index arrays and layer name arrays.

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

    Names assigned to the network layers created by the grouping, specified as a character vector or string scalar for a single network layer, or a string array for multiple network layers. The names must be unique and the number of names specified must equal the number of groups.

    Specifying names is only supported when you specify the groups using the groups input.

    The default name for a single network layer is "subnet". The default names for multiple network layers are "subnet_1",...,"subnet_N".

    Example: ["customTransformerLayer_1" "customTransformerLayer_2"]

    Data Types: char | string

    The delimiter for grouping layers by pattern, specified as a string scalar or character vector.

    When grouping layers by pattern, the software searches for the delimiter and groups layers whose names start with same pattern. For example, when using the default delimiter ":", the software groups layers named "subnet:conv" and "subnet:relu" into a single network layer named "subnet".

    The delimiter cannot contain forward slashes "/" as forward slashes are reserved to define paths.

    Data Types: char | string

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

    • If tf is 1 (true), then for each layer in net, the software groups that layer into a network layer for every instance of the "xxx:" pattern found in the layer name. For example, the software groups a layer named "subnet:nestedNet:conv" into a network layer named "nestedNet" that is nested inside a network layer named "subnet".

    • If tf is 0 (false), then for each layer in net, the software groups that layer into a single network layer defined by the first instance of the "xxx:" pattern found in the layer name. For example, the software groups a layer named "subnet:nestedNet:conv" into a network layer named "subnet".

    Data Types: logical

    Output Arguments

    collapse all

    Updated network, returned as a dlnetwork object.

    Version History

    Introduced in R2024a