Main Content

addLayers

Add layers to neural network

Description

netUpdated = addLayers(net,layers) adds the network layers in layers to the dlnetwork object net. The updated network netUpdated contains the layers and connections of net together with the layers in layers, connected sequentially. The layer names in layers 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
plot(net)

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.

net = dlnetwork;

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
plot(net)

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

Input Arguments

collapse all

Neural network, specified as a dlnetwork object.

Network layers, specified as a Layer array.

For a list of built-in layers, see List of Deep Learning Layers.

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