Main Content

networkLayer

Network Layer

Since R2024a

Description

A network layer contains a nested network. Use network layers to simplify building large networks that contain repeating components.

During training and inference a network layer behaves identically to the nested network.

Tip

To visualize and edit layers in a network layer using Deep Network Designer, expand the network using the expandLayers function before opening the network in Deep Network Designer. After editing the network and exporting it to the workspace, you can regroup the layers into network layers using the groupLayers function. Adding a network layer to a network in Deep Network Designer is not supported.

Creation

Description

example

layer = networkLayer(net) creates a network layer containing the neural network net.

example

layer = networkLayer(___,Name=Value) sets additional Name and OutputNames properties using one or more name-value arguments and any of the previous syntaxes.

Input Arguments

expand all

Neural network, specified as a dlnetwork object or a layer array.

Properties

expand all

This property is read-only.

Nested network, specified as a dlnetwork object.

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

The NetworkLayer object stores this property as a character vector.

Data Types: char | string

This property is read-only.

Names of the inputs of the layer, specified as a cell array of character vectors.

Layer inputs are the unconnected inputs of the layers in the nested network.

For layers with a single input, the input name is the name of the layer. For layers with multiple inputs, the input name is "layerName/inputName", where layerName is the name of the layer and inputName is the name of the layer input.

Data Types: cell

Names of the outputs of the layer, specified as a cell array of character vectors.

If you do not specify the output names, then the output names are the same as those of the nested network.

For layers with a single output, the output name is the name of the layer. For layers with multiple outputs, the output name is "layerName/outputName", where layerName is the name of the layer and outputName is the name of the layer output.

The predict and forward functions, by default, return the data output by the layers given by the OutputNames property.

Data Types: cell

Examples

collapse all

Create an array of layers containing an lstmLayer with 100 hidden units and a dropoutLayer with a dropout probability of 0.2.

layers = [lstmLayer(100,OutputMode="sequence")
    dropoutLayer(0.2)];

Create a network layer containing these layers.

lstmDropoutLayer = networkLayer(layers)
lstmDropoutLayer = 
  NetworkLayer with properties:

           Name: ''
     InputNames: {'lstm'}
    OutputNames: {'dropout'}

   Learnable Parameters
        Network: [1x1 dlnetwork]

   State Parameters
        Network: [1x1 dlnetwork]

Use properties method to see a list of all properties.

Inspect the layers of the network layer. This is equivalent to calling lstmDropoutLayer.Network.Layers.

lstmDropoutLayer.Layers
ans = 
  2x1 Layer array with layers:

     1   'lstm'      LSTM      LSTM with 100 hidden units
     2   'dropout'   Dropout   20% dropout

Use the network layer to build a network which you can train using the trainnet function.

layers = [sequenceInputLayer(3)
    lstmDropoutLayer
    lstmDropoutLayer
    fullyConnectedLayer(10)
    softmaxLayer];

Download a pretrained image classification network.

resnet = imagePretrainedNetwork("resnet50");

Load a test image.

img = imread("peppers.png");
figure
imshow(img)

Use the pretrained network to create a network layer by specifying intermediate layers as the outputs of the network layer. This network layer can be used as a feature extractor, for example as the backbone of an object detection network such as a Mask R-CNN object detector.

extractorLayer = networkLayer(resnet,OutputNames=["activation_10_relu" "activation_22_relu" "activation_40_relu" "activation_49_relu"]);

Create a dlnetwork object containing the feature extractor and initialize the network using the test image.

img = dlarray(single(img),"SSC");
net = dlnetwork(extractorLayer,img);

Use the predict function to compute the outputs of network.

[output1,output2,output3,output4] = predict(net,img);
extractedFeatures = {output1 output2 output3 output4};

Average the extracted features over the channel dimension and plot the extracted features.

figure
t = tiledlayout("flow");
title(t,"Feature Maps")
for idx=1:numel(extractedFeatures)
    features = mean(extractdata(extractedFeatures{idx}),3);
    resolution = size(features);
    nexttile
    imagesc(features)
    title("Resolution: " + resolution(1) + "-by-" + resolution(2))
    axis off
end

More About

expand all

Version History

Introduced in R2024a