replaceLayer - Replace layer in neural network - MATLAB (original) (raw)

Replace layer in neural network

Syntax

Description

[netUpdated](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5F5956ea1d-4747-411e-8acc-7424b78ab717) = replaceLayer([net](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layerName](#d126e225840),[layers](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5Fe02df97c-78b7-4b02-a434-29a7a4198b64)) replaces the layer layerName in the dlnetwork object net with the layers in layers.

replaceLayer connects the layers inlayers sequentially and connectslayers into the network.

example

[netUpdated](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5F5956ea1d-4747-411e-8acc-7424b78ab717) = replaceLayer([net](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layerPath](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5Fae951fe5-a0b3-4363-9d2e-5f65cd97c1da),[layers](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5Fe02df97c-78b7-4b02-a434-29a7a4198b64)) replaces the layer specified by layerPath in thedlnetwork object net with the layers inlayers. (since R2024a)

example

[netUpdated](#mw%5F9be7baf8-39c4-463f-84f2-6fe81ce4df45%5Fsep%5Fmw%5F5956ea1d-4747-411e-8acc-7424b78ab717) = replaceLayer(___,ReconnectBy=[mode](#mw%5F2257abc8-4860-4b96-85dd-f2eb92a7f1b1)) additionally specifies the method of reconnecting layers.

Examples

collapse all

Define a simple network architecture and display the network in a plot.

net = dlnetwork;

layers = [ imageInputLayer([28 28 1],Name="input") convolution2dLayer(3,16,Padding="same") reluLayer(Name="relu1") additionLayer(2,Name="add") fullyConnectedLayer(10) softmaxLayer];

net = addLayers(net,layers); net = connectLayers(net,"input","add/in2");

figure plot(net)

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

Replace the ReLU layer in the network with a batch normalization layer followed by a leaky ReLU layer.

layers = [ batchNormalizationLayer leakyReluLayer(0.1)];

net = replaceLayer(net,"relu1",layers);

Plot the updated network.

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

Define a network architecture containing several network layers.

net = dlnetwork;

layers = [lstmLayer(100,OutputMode="sequence") dropoutLayer(0.2)];

lstmDropoutLayer = networkLayer(layers);

layers = [sequenceInputLayer(3) lstmDropoutLayer lstmDropoutLayer fullyConnectedLayer(10) softmaxLayer];

net = addLayers(net,layers);

Plot the network.

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

Look up the first LSTM layer by specifying the path to the layer. The path includes the name of the network layer ("subnet_1") and the name of the LSTM layer ("lstm"), separated by a forward slash. You can use the path to look up layers nested within a hierarchy of multiple network layers.

tempLSTMLayer = getLayer(net,"subnet_1/lstm");

Edit properties of the layer, and replace the original layer with the modified layer.

tempLSTMLayer.InputWeightsInitializer = "zeros"; tempLSTMLayer.RecurrentWeightsInitializer = "zeros"; tempLSTMLayer.BiasInitializer = "ones"; tempLSTMLayer.Name = "modifiedLSTM";

Replace the original layer with the modified layer, specifying the network, the path to the layer to replace, and the modified layer.

net = replaceLayer(net,"subnet_1/lstm",tempLSTMLayer);

Input Arguments

collapse all

Neural network, specified as a dlnetwork object.

Name of the layer to replace, specified as a string scalar or a character vector.

Path to nested layer, specified as a character vector or string scalar.

For a layer within a networkLayer, specify layerPath as:

Custom layers can have a dlnetwork as a learnable or state property. For a layer of a dlnetwork in the property of a custom layer, specifylayerPath as:

Data Types: char | string

Method to reconnect layers specified as one of the following:

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 replaceLayer 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 R2018b

expand all

Starting in R2024a, you can now replace a layer nested inside a networkLayer or inside the property of a custom layer by specifying the path to the nested layer. To replace a nested layer, use the newlayerPath input.

Starting in R2024a, LayerGraph objects are not recommended. Usedlnetwork objects instead. This recommendation means that these syntaxes are 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.