expandLayers - Expand network layers - MATLAB (original) (raw)

Expand network layers

Since R2024a

Syntax

Description

[netUpdated](#mw%5F3b56e101-7db7-4e10-a7a9-be1c029e6a56) = expandLayers([net](#mw%5Fa8494f36-35d0-4d76-869f-6ea0b0deca59)) 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](#mw%5F3b56e101-7db7-4e10-a7a9-be1c029e6a56) = expandLayers([net](#mw%5Fa8494f36-35d0-4d76-869f-6ea0b0deca59),[layerNames](#mw%5F595e68f2-a1e7-4588-a779-fbf887cc8c05)) expands only the network layers specified by name.

[netUpdated](#mw%5F3b56e101-7db7-4e10-a7a9-be1c029e6a56) = expandLayers([net](#mw%5Fa8494f36-35d0-4d76-869f-6ea0b0deca59),[layerIndices](#mw%5F560ea507-edd1-4e76-9b61-4ebffe3781b8)) expands only the network layers specified by index.

example

[netUpdated](#mw%5F3b56e101-7db7-4e10-a7a9-be1c029e6a56) = expandLayers(___,[Name=Value](#namevaluepairarguments)) 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.

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.

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

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

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

collapse all

Specify optional pairs of arguments asName1=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) or0 (false).

Data Types: logical

Version History

Introduced in R2024a