This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Graph of network layers for deep learning


A layer graph specifies the architecture of a deep learning network with a more complex graph structure in which layers can have inputs from multiple layers and outputs to multiple layers. Networks with this structure are called directed acyclic graph (DAG) networks. After you create a layerGraph object, you can use the object functions to plot the graph and modify it by adding, removing, connecting, and disconnecting layers. To train the network, use the layer graph as the layers input argument to trainNetwork.



lgraph = layerGraph
lgraph = layerGraph(layers)
lgraph = layerGraph(dagNet)



lgraph = layerGraph creates an empty layer graph that contains no layers. You can add layers to the empty graph by using the addLayers function.


lgraph = layerGraph(layers) creates a layer graph from an array of network layers and sets the Layers property. The layers in lgraph are connected in the same sequential order as in layers. All layers must have unique, nonempty names.


lgraph = layerGraph(dagNet) extracts the layer graph of the DAG network dagNet. For example, you can extract the layer graph of a pretrained network to perform transfer learning.

Input Arguments

expand all

DAG network, specified as a DAGNetwork object.


expand all

Network layers, specified as a Layer array.

Layer connections, specified as a table with two columns.

Each table row represents a connection in the layer graph. The first column, Source, specifies the source of each connection. The second column, Destination, specifies the destination of each connection. The connection sources and destinations are either layer names or have the form 'layerName/IOName', where 'IOName' is the name of the layer input or output.

Data Types: table

Object Functions

addLayersAdd layers to layer graph
removeLayersRemove layers from layer graph
replaceLayerReplace layer in layer graph
connectLayersConnect layers in layer graph
disconnectLayersDisconnect layers in layer graph
plotPlot neural network layer graph


collapse all

Create an empty layer graph and an array of layers. Add the layers to the layer graph and plot the graph. addLayers connects the layers sequentially.

lgraph = layerGraph;

layers = [
    imageInputLayer([32 32 3],'Name','input')  

lgraph = addLayers(lgraph,layers);

Create an array of layers.

layers = [
    imageInputLayer([28 28 1],'Name','input')  

Create a layer graph from the layer array. layerGraph connects all the layers in layers sequentially. Plot the layer graph.

lgraph = layerGraph(layers);

Load a pretrained squeezenet network. If the Deep Learning Toolbox™ Model for SqueezeNet Network support package is not installed, then the software provides a download link. You can use this trained network for classification and prediction.

net = squeezenet;

To modify the network structure, first extract the structure of the DAG network by using layerGraph. You can then use the object functions of LayerGraph to modify the network architecture.

lgraph = layerGraph(net)
lgraph = 
  LayerGraph with properties:

         Layers: [68x1 nnet.cnn.layer.Layer]
    Connections: [75x2 table]

Create a simple directed acyclic graph (DAG) network for deep learning. Train the network to classify images of digits. The simple network in this example consists of:

  • A main branch with layers connected sequentially.

  • A shortcut connection containing a single 1-by-1 convolutional layer. Shortcut connections enable the parameter gradients to flow more easily from the output layer to the earlier layers of the network.

Create the main branch of the network as a layer array. The addition layer sums multiple inputs element-wise. Specify the number of inputs for the addition layer to sum. All layers must have names and all names must be unique.

layers = [
    imageInputLayer([28 28 1],'Name','input')

Create a layer graph from the layer array. layerGraph connects all the layers in layers sequentially. Plot the layer graph.

lgraph = layerGraph(layers);

Create the 1-by-1 convolutional layer and add it to the layer graph. Specify the number of convolutional filters and the stride so that the activation size matches the activation size of the 'relu_3' layer. This arrangement enables the addition layer to add the outputs of the 'skipConv' and 'relu_3' layers. To check that the layer is in the graph, plot the layer graph.

skipConv = convolution2dLayer(1,32,'Stride',2,'Name','skipConv');
lgraph = addLayers(lgraph,skipConv);

Create the shortcut connection from the 'relu_1' layer to the 'add' layer. Because you specified two as the number of inputs to the addition layer when you created it, the layer has two inputs named 'in1' and 'in2'. The 'relu_3' layer is already connected to the 'in1' input. Connect the 'relu_1' layer to the 'skipConv' layer and the 'skipConv' layer to the 'in2' input of the 'add' layer. The addition layer now sums the outputs of the 'relu_3' and 'skipConv' layers. To check that the layers are connected correctly, plot the layer graph.

lgraph = connectLayers(lgraph,'relu_1','skipConv');
lgraph = connectLayers(lgraph,'skipConv','add/in2');

Load the training and validation data, which consists of 28-by-28 grayscale images of digits.

[XTrain,YTrain] = digitTrain4DArrayData;
[XValidation,YValidation] = digitTest4DArrayData;

Specify training options and train the network. trainNetwork validates the network using the validation data every ValidationFrequency iterations.

options = trainingOptions('sgdm', ...
    'MaxEpochs',8, ...
    'Shuffle','every-epoch', ...
    'ValidationData',{XValidation,YValidation}, ...
    'ValidationFrequency',30, ...
    'Verbose',false, ...
net = trainNetwork(XTrain,YTrain,lgraph,options);

Display the properties of the trained network. The network is a DAGNetwork object.

net = 
  DAGNetwork with properties:

         Layers: [16×1 nnet.cnn.layer.Layer]
    Connections: [16×2 table]

Classify the validation images and calculate the accuracy. The network is very accurate.

YPredicted = classify(net,XValidation);
accuracy = mean(YPredicted == YValidation)
accuracy = 0.9968


  • Layer graphs cannot specify the architecture of long short-term memory (LSTM) networks. For more information on how to create an LSTM network, see Long Short-Term Memory Networks.

Introduced in R2017b