connectLayers
Connect layers in neural network
Description
connects the source layer netUpdated = connectLayers(net,s,d)s to the destination layer
d in the dlnetwork object
net. The updated network, netUpdated,
contains the same layers as net and includes the new
connection.
Examples
Create an empty neural network dlnetwork object and add an addition layer with two inputs and the name 'add'.
net = dlnetwork; layer = additionLayer(2,'Name','add'); net = addLayers(net,layer);
Add two ReLU layers to the neural network and connect them to the addition layer. The addition layer outputs the sum of the outputs from the ReLU layers.
layer = reluLayer('Name','relu1'); net = addLayers(net,layer); net = connectLayers(net,'relu1','add/in1'); layer = reluLayer('Name','relu2'); net = addLayers(net,layer); net = connectLayers(net,'relu2','add/in2');
Visualize the updated network in a plot.
plot(net)

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)

Input Arguments
Neural network, specified as a dlnetwork object.
Connection source, specified as a character vector or a string scalar.
If the source layer has a single output, then
sis the name of the layer.If the source layer has multiple outputs, then
sis the layer name followed by the"/"character and the name of the layer output:"layerName/outputName".
Example: "conv"
Example: "mpool/indices"
Connection destination, specified as a string scalar or a character vector.
If the destination layer has a single input, then
dis the name of the layer.If the destination layer has multiple inputs, then
dis the layer name followed by the"/"character and the name of the layer input:"layerName/inputName".
Example: "fc"
Example: "add/in1"
Output Arguments
Updated network, returned as an uninitialized dlnetwork object.
To initialize the learnable parameters of a dlnetwork object, use the initialize function.
The connectLayers 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 R2017bStarting in R2024a, LayerGraph objects are not recommended. Use
dlnetwork objects instead. This
recommendation means that this syntax is not recommended for
LayerGraph input:
lgraphUpdated = connectLayers(lgraph,s,d)
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 Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)