Main Content


(Not recommended) Graph of network layers for deep learning

LayerGraph objects are not recommended. Use dlnetwork objects instead. For more information, see Version History.


A layer graph specifies the architecture of a neural network as a directed acyclic graph (DAG) of deep learning layers. The layers can have multiple inputs and multiple outputs.




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.

lgraph = layerGraph(net) extracts the layer graph of a SeriesNetwork, DAGNetwork, or dlnetwork object. For example, you can extract the layer graph of a pretrained network to perform transfer learning.

Input Arguments

expand all

Deep learning network, specified as a SeriesNetwork, DAGNetwork, or dlnetwork object.


expand all

This property is read-only.

Network layers, specified as a Layer array.

This property is read-only.

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

This property is read-only.

Names of the input layers, specified as a cell array of character vectors.

Data Types: cell

This property is read-only.

Names of the output layers, specified as a cell array of character vectors.

Data Types: cell

Object Functions

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


collapse all

Create a simple layer graph for deep learning.

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. To easily add connections later, specify names for the first ReLU layer and the addition layer.

layers = [
    imageInputLayer([28 28 1])

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

lgraph = layerGraph(layers);

Figure contains an axes object. The axes object contains an object of type graphplot.

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 third ReLU layer. This arrangement enables the addition layer to add the outputs of the third ReLU layer and the 1-by-1 convolutional layer. 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);

Figure contains an axes object. The axes object contains an object of type graphplot.

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 third ReLU 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 third ReLU layer and the 'skipConv' layer. To check that the layers are connected correctly, plot the layer graph.

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

Figure contains an axes object. The axes object contains an object of type graphplot.


  • Layer graph objects contain no quantization information. Extracting the layer graph from a quantized network and then reassembling the network using assembleNetwork or dlnetwork removes quantization information from the network.

Version History

Introduced in R2017b

collapse all

R2024a: Not recommended

Starting in R2024a, LayerGraph objects are not recommended, use dlnetwork objects instead.

There are no plans to remove support for LayerGraph objects. However, dlnetwork objects have these advantages and are recommended instead:

  • dlnetwork objects are a unified data type that supports network building, prediction, built-in training, visualization, compression, verification, and custom training loops.

  • dlnetwork objects support a wider range of network architectures that you can create or import from external platforms.

  • The trainnet function supports dlnetwork objects, which enables you to easily specify loss functions. You can select from built-in loss functions or specify a custom loss function.

  • Training and prediction with dlnetwork objects is typically faster than LayerGraph and trainNetwork workflows.

Most functions that support LayerGraph objects also support dlnetwork objects. This table shows some typical usages of LayerGraph objects and how to update your code to use dlnetwork object functions instead.

Not RecommendedRecommended
lgraph = layerGraph;net = dlnetwork;
lgraph = layerGraph(layers);net = dlnetwork(layers,Initialize=false);
lgraph = layerGraph(net);net = dag2dlnetwork(net);
lgraph = addLayers(lgraph,layers);net = addLayers(net,layers);
lgraph = removeLayers(lgraph,layerNames);net = removeLayers(net,layerNames);
lgraph = replaceLayer(lgraph,layerName,layers);net = replaceLayer(net,layerName,layers);
lgraph = connectLayers(lgraph,s,d);net = connectLayers(net,s,d);
lgraph = disconnectLayers(lgraph,s,d);net = disconnectLayers(net,s,d);

To train a neural network specified as a dlnetwork object, use the trainnet function.

In a LayerGraph object, instead of using an output layer, specify the loss function using the loss function argument of the trainnet function.