groupLayers - Group layers into network layers - MATLAB (original) (raw)

Group layers into network layers

Since R2024a

Syntax

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

[netUpdated](#mw%5F432d7fe3-abd6-45a8-8919-1a958a560f9c) = groupLayers([net](#mw%5F637aac4f-dada-42e9-a3cc-7f194b97fd7c)) 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](#mw%5F432d7fe3-abd6-45a8-8919-1a958a560f9c) = groupLayers([net](#mw%5F637aac4f-dada-42e9-a3cc-7f194b97fd7c),Delimiter=[delim](#mw%5F8bd02bce-4c67-4e7c-83e2-49e285e2104b)) specifies the delimiter for grouping by pattern. For example, specifyingDelimiter="@" groups layers whose names contain the pattern"subnet@" instead of "subnet:".

example

[netUpdated](#mw%5F432d7fe3-abd6-45a8-8919-1a958a560f9c) = groupLayers(___,Recursive=[tf](#mw%5Fa8079b62-9edc-4357-b996-475652d65cda)) 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

[netUpdated](#mw%5F432d7fe3-abd6-45a8-8919-1a958a560f9c) = groupLayers([net](#mw%5F637aac4f-dada-42e9-a3cc-7f194b97fd7c),[groups](#mw%5F314a3d0b-da1c-43ff-b656-a9cff61a6182),GroupNames=[Names](#mw%5F5051872a-e6e6-4439-aec9-5c214a7d3a17)) groups layers in net specified by groups intonetworkLayer objects. The names of the network layers are specified by Names.

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.

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.

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 = 4×1 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 = 8×1 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.

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 = 4×1 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.

ans = 6×1 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, specifygroups as one of the following:

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

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 thegroups 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) or0 (false).

Data Types: logical

Version History

Introduced in R2024a