replaceLayer
Replace layer in neural network
Syntax
Description
replaces the layer netUpdated
= replaceLayer(net
,layerName
,layers
)layerName
in the dlnetwork
object net
with the layers in layers
.
replaceLayer
connects the layers in
layers
sequentially and connects
layers
into the network.
replaces the layer specified by netUpdated
= replaceLayer(net
,layerPath
,layers
)layerPath
in the
dlnetwork
object net
with the layers in
layers
. (since R2024a)
additionally specifies the method of reconnecting layers.netUpdated
= replaceLayer(___,ReconnectBy=mode
)
Examples
Replace Layer in Neural Network
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)
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 plot(net)
Replace Layer in Nested Network
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 plot(net)
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
net
— Neural network
dlnetwork
object
Neural network, specified as a dlnetwork
object.
layerName
— Name of layer to replace
string scalar | character vector
Name of the layer to replace, specified as a string scalar or a character vector.
layers
— Network layers
Layer
array
Network layers, specified as a Layer
array.
For a list of built-in layers, see List of Deep Learning Layers.
layerPath
— Path to nested layer
character vector | string scalar
Path to nested layer, specified as a character vector or string scalar.
For a layer within a networkLayer
,
specify layerPath
as:
The name of the network layer and the name of the nested layer separated by a forward slash
"/"
. For example, the path to a layer named"nestedLayerName"
in a network layer named"networkLayerName"
is"networkLayerName/nestedLayerName"
. If there are multiple levels of nested layers, then specify the path using the formnetworkLayerName1/.../networkLayerNameN/nestedLayerName
.
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, specify
layerPath
as:
The name of the custom layer, the name of the
dlnetwork
property, and the name of the nested layer separated by forward slashes"/"
. For example, the path to a layer named"layerName"
inside adlnetwork
property named"propertyName"
of a custom layer named"customLayerName"
is"customLayerName/propertyName/layerName"
. If there are multiple levels of nested layers, then specify the path using the form"customLayerName1/propertyName1/.../customLayerNameN/propertyNameN/layerName"
.
Data Types: char
| string
mode
— Method to reconnect layers
"name"
(default) | "order"
Method to reconnect layers specified as one of the following:
"name"
– Reconnectlayers
using the input and output names of the replaced layer. For each layer connected to an input of the replaced layer, reconnect the layer to the input of the same input name oflayers(1)
. For each layer connected to an output of the replaced layer, reconnect the layer to the output of the same output name oflayers(end)
."order"
– Reconnectlayers
using the order of the input names oflayers(1)
and the output names oflayers(end)
. Reconnect the layer connected to thei
th input of the replaced layer to thei
th input oflayers(1)
. Reconnect the layer connected to thej
th output of the replaced layer to thej
th output oflayers(end)
.
Output Arguments
netUpdated
— Updated network
dlnetwork
object
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 R2018bR2024a: Replace nested layers
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 new
layerPath
input.
R2024a: LayerGraph
objects are not recommended
Starting in R2024a, LayerGraph
objects are not recommended. Use
dlnetwork
objects instead. This
recommendation means that these syntaxes are not recommended for
LayerGraph
input:
lgraphUpdated = replaceLayer(lgraph,layerName,layers)
lgraphUpdated = replaceLayer(lgraph,layerName,layers,ReconnectBy=mode)
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 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.
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)