addLayers - Add layers to neural network - MATLAB (original) (raw)

Add layers to neural network

Syntax

Description

[netUpdated](#mw%5F25b609b6-6b6a-4ab9-9353-09f6efd0bf74%5Fsep%5Fmw%5F5956ea1d-4747-411e-8acc-7424b78ab717) = addLayers([net](#mw%5F25b609b6-6b6a-4ab9-9353-09f6efd0bf74%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layers](#mw%5F25b609b6-6b6a-4ab9-9353-09f6efd0bf74%5Fsep%5Fmw%5Fe02df97c-78b7-4b02-a434-29a7a4198b64)) adds the network layers in layers to thedlnetwork object net. The updated networknetUpdated contains the layers and connections ofnet together with the layers inlayers, connected sequentially. The layer names inlayers must be unique, nonempty, and different from the names of the layers in net.

example

Examples

collapse all

Create an empty neural network and an array of layers. The addLayers function connects the layers sequentially.

net = dlnetwork;

layers = [ imageInputLayer([32 32 3])
convolution2dLayer(3,16,Padding="same") batchNormalizationLayer reluLayer];

net = addLayers(net,layers);

View the neural network in a plot.

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

Define a two-output neural network that predicts both categorical labels and numeric values given 2-D images as input.

Specify the number of classes and responses.

numClasses = 10; numResponses = 1;

Create an empty neural network.

Define the layers of the main branch of the network and the softmax output.

layers = [ imageInputLayer([28 28 1],Normalization="none")

convolution2dLayer(5,16,Padding="same")
batchNormalizationLayer
reluLayer(Name="relu_1")

convolution2dLayer(3,32,Padding="same",Stride=2)
batchNormalizationLayer
reluLayer
convolution2dLayer(3,32,Padding="same")
batchNormalizationLayer
reluLayer

additionLayer(2,Name="add")

fullyConnectedLayer(numClasses)
softmaxLayer(Name="softmax")];

net = addLayers(net,layers);

Add the skip connection.

layers = [ convolution2dLayer(1,32,Stride=2,Name="conv_skip") batchNormalizationLayer reluLayer(Name="relu_skip")];

net = addLayers(net,layers); net = connectLayers(net,"relu_1","conv_skip"); net = connectLayers(net,"relu_skip","add/in2");

Add the fully connected layer for the regression output.

layers = fullyConnectedLayer(numResponses,Name="fc_2"); net = addLayers(net,layers); net = connectLayers(net,"add","fc_2");

View the neural network in a plot.

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

Output Arguments

collapse all

Updated network, returned as an uninitialized dlnetwork object.

To initialize the learnable parameters of a dlnetwork object, use the initialize function.

The addLayers function does not preserve quantization information. If the input network is a quantized network, then the output network does not contain quantization information.

Version History

Introduced in R2017b

expand all

Starting in R2024a, LayerGraph objects are not recommended. Usedlnetwork objects instead. This recommendation means that this syntax is not recommended forLayerGraph input:

Most functions that support LayerGraph objects also supportdlnetwork objects. This table shows some typical usages ofLayerGraph objects and how to update your code to usedlnetwork object functions instead.

Not Recommended Recommended
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);
plot(lgraph); plot(net);

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