NetworkLayer - Network Layer - MATLAB (original) (raw)
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 a network layer, use Deep Network Designer. To see inside the layer, double click the layer. To edit the layers in Deep Network Designer, you must first expand the network using the expandLayers function before opening the network in the app. 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
Syntax
Description
`layer` = networkLayer([net](#mw%5F3ab97563-1079-485a-9dec-818214c0bcb7))
creates a network layer containing the neural network net
.
`layer` = networkLayer(___,[Name=Value](#namevaluepairarguments))
sets writable properties using on or more name-value arguments. For example,Name="myNetworkLayer"
sets the name of the network layer.
Input Arguments
Neural network, specified as a dlnetwork
object or a layer array.
Name-Value Arguments
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: layer = networkLayer(net,Name="myNetworkLayer")
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 ""
.
This argument sets the Name property as a character vector.
Data Types: char
| string
Names of the layer outputs, 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.
This argument sets the OutputNames property.
Data Types: cell
Properties
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 layer inputs, 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 layer outputs, 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
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: [1×1 dlnetwork]
State Parameters Network: [1×1 dlnetwork]
Show all properties
Inspect the layers of the network layer. This is equivalent to calling lstmDropoutLayer.Network.Layers
.
ans = 2×1 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
Create an array of layers.
convLayers = [ ... convolution1dLayer(5,16,Padding="causal") reluLayer layerNormalizationLayer];
Use networkLayer
to create a convolutional block of layers.
convBlock = networkLayer(convLayers);
Define a network containing two convolutional blocks.
layers = [ ... sequenceInputLayer(10) convBlock convBlock globalAveragePooling1dLayer fullyConnectedLayer(3) softmaxLayer];
Visualize the network using the Deep Network Designer app.
deepNetworkDesigner(layers)
To see inside the network layer, double click the layer.
To navigate up in the hierarchy, in the bar that appears above the canvas, click the name of the network whose level you want to see.
To edit the layers in Deep Network Designer, you must first expand the network using the expandLayers
function before opening the network in the app. 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.
More About
To generate code for a trained network containing network layers, first expand the network using the expandLayers function.
Version History
Introduced in R2024a