function lgraph = layerGraph(layers)
% layerGraph   Layer graph
%
%   lgraph = layerGraph creates an empty layer graph.
%
%   lgraph = layerGraph(larray) creates a layer graph from the layer
%   array larray by connecting the layers in series. The layer
%   names in the layer array must be unique or empty. In case of empty
%   names, the function automatically assigns layer names.
%
%   lgraph = layerGraph(net) extracts the layer graph from the
%   SeriesNetwork, DAGNetwork, or dlnetwork object net. This is useful for
%   transfer learning.
%
%   Example:
%       Create a layer graph that defines a network with a skip connection.
%
%       layers = [
%           imageInputLayer([28 28 1])
%           convolution2dLayer(5, 20)
%           reluLayer('Name', 'relu_1')
%           convolution2dLayer(3, 20, 'Padding', 1)
%           reluLayer
%           convolution2dLayer(3, 20, 'Padding', 1)
%           reluLayer
%           additionLayer(2, 'Name', 'add')
%           fullyConnectedLayer(10)
%           softmaxLayer
%           classificationLayer];
%
%       lgraph = layerGraph(layers);
%
%       lgraph = connectLayers(lgraph, 'relu_1', 'add/in2');
%
%   See also nnet.cnn.LayerGraph.

%   Copyright 2017-2022 The MathWorks, Inc.

arguments
    layers {iValidateLayers(layers)} = nnet.cnn.layer.Layer.empty(0,1)
end

try
    lgraph = nnet.cnn.LayerGraph(layers, ValidateLayers=true);
catch me
    throw(me)
end

end

function iValidateLayers(layers)
if ~isa(layers, 'nnet.cnn.layer.Layer')
    error(message('nnet_cnn:layerGraph:InvalidLayerArrayOrNetwork'));
end
end