additionLayer
Addition layer
Description
An addition layer adds inputs from multiple neural network layers element-wise.
Specify the number of inputs to the layer when you create it. The inputs to the layer
have the names 'in1','in2',...,'inN'
, where N
is
the number of inputs. Use the input names when connecting or disconnecting the layer by
using connectLayers
or disconnectLayers
. All inputs to an addition layer must have the same
dimension.
Creation
Properties
NumInputs
— Number of inputs
positive integer
Number of inputs to the layer, specified as a positive integer greater than or equal to 2.
The inputs have the names 'in1','in2',...,'inN'
, where N
is
NumInputs
. For example, if NumInputs
is 3,
then the inputs have the names 'in1','in2'
, and 'in3'
. Use the input names when
connecting or disconnecting the layer using the connectLayers
or disconnectLayers
functions.
Name
— Layer name
""
(default) | character vector | string scalar
Layer name, specified as a character vector or a string scalar.
For Layer
array input, the trainnet
, trainNetwork
, assembleNetwork
, layerGraph
, and
dlnetwork
functions automatically assign
names to layers with the name ""
.
The AdditionLayer
object stores this property as a character vector.
Data Types: char
| string
InputNames
— Input Names
{'in1','in2',…,'inN'}
(default)
Input names, specified as {'in1','in2',...,'inN'}
, where N
is the number of inputs of the layer.
Data Types: cell
NumOutputs
— Number of outputs
1
(default)
This property is read-only.
Number of outputs from the layer, returned as 1
. This layer has a
single output only.
Data Types: double
OutputNames
— Output names
{'out'}
(default)
This property is read-only.
Output names, returned as {'out'}
. This layer has a single output
only.
Data Types: cell
Examples
Create and Connect Addition Layer
Create an addition layer with two inputs and the name 'add_1'
.
add = additionLayer(2,'Name','add_1')
add = AdditionLayer with properties: Name: 'add_1' NumInputs: 2 InputNames: {'in1' 'in2'}
Create two ReLU layers and connect them to the addition layer. The addition layer sums the outputs from the ReLU layers.
relu_1 = reluLayer('Name','relu_1'); relu_2 = reluLayer('Name','relu_2'); lgraph = layerGraph; lgraph = addLayers(lgraph,relu_1); lgraph = addLayers(lgraph,relu_2); lgraph = addLayers(lgraph,add); lgraph = connectLayers(lgraph,'relu_1','add_1/in1'); lgraph = connectLayers(lgraph,'relu_2','add_1/in2'); plot(lgraph)
Create Simple DAG Network
Create a simple directed acyclic graph (DAG) network for deep learning. Train the network to classify images of digits. The simple network in this example consists of:
A main branch with layers connected sequentially.
A shortcut connection containing a single 1-by-1 convolutional layer. Shortcut connections enable the parameter gradients to flow more easily from the output layer to the earlier layers of the network.
Create the main branch of the network as a layer array. The addition layer sums multiple inputs element-wise. Specify the number of inputs for the addition layer to sum. To easily add connections later, specify names for the first ReLU layer and the addition layer.
layers = [ imageInputLayer([28 28 1]) 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') averagePooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer classificationLayer];
Create a layer graph from the layer array. layerGraph
connects all the layers in layers
sequentially. Plot the layer graph.
lgraph = layerGraph(layers); figure plot(lgraph)
Create the 1-by-1 convolutional layer and add it to the layer graph. Specify the number of convolutional filters and the stride so that the activation size matches the activation size of the third ReLU layer. This arrangement enables the addition layer to add the outputs of the third ReLU layer and the 1-by-1 convolutional layer. To check that the layer is in the graph, plot the layer graph.
skipConv = convolution2dLayer(1,32,'Stride',2,'Name','skipConv'); lgraph = addLayers(lgraph,skipConv); figure plot(lgraph)
Create the shortcut connection from the 'relu_1'
layer to the 'add'
layer. Because you specified two as the number of inputs to the addition layer when you created it, the layer has two inputs named 'in1'
and 'in2'
. The third ReLU layer is already connected to the 'in1'
input. Connect the 'relu_1'
layer to the 'skipConv'
layer and the 'skipConv'
layer to the 'in2'
input of the 'add'
layer. The addition layer now sums the outputs of the third ReLU layer and the 'skipConv'
layer. To check that the layers are connected correctly, plot the layer graph.
lgraph = connectLayers(lgraph,'relu_1','skipConv'); lgraph = connectLayers(lgraph,'skipConv','add/in2'); figure plot(lgraph);
Load the training and validation data, which consists of 28-by-28 grayscale images of digits.
[XTrain,YTrain] = digitTrain4DArrayData; [XValidation,YValidation] = digitTest4DArrayData;
Specify training options and train the network. trainNetwork
validates the network using the validation data every ValidationFrequency
iterations.
options = trainingOptions('sgdm', ... 'MaxEpochs',8, ... 'Shuffle','every-epoch', ... 'ValidationData',{XValidation,YValidation}, ... 'ValidationFrequency',30, ... 'Verbose',false, ... 'Plots','training-progress'); net = trainNetwork(XTrain,YTrain,lgraph,options);
Display the properties of the trained network. The network is a DAGNetwork
object.
net
net = DAGNetwork with properties: Layers: [16x1 nnet.cnn.layer.Layer] Connections: [16x2 table] InputNames: {'imageinput'} OutputNames: {'classoutput'}
Classify the validation images and calculate the accuracy. The network is very accurate.
YPredicted = classify(net,XValidation); accuracy = mean(YPredicted == YValidation)
accuracy = 0.9934
Algorithms
Layer Input and Output Formats
Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray
objects. The format of a dlarray
object is a string of characters, in which each character describes the corresponding dimension of the data. The formats consists of one or more of these characters:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, 2-D image data represented as a 4-D array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, can be described as having the format "SSCB"
(spatial, spatial, channel, batch).
You can interact with these dlarray
objects in automatic differentiation workflows such as developing a custom layer, using a functionLayer
object, or using the forward
and predict
functions with dlnetwork
objects.
This table shows the supported input formats of AdditionLayer
objects and the
corresponding output format. If the software passes the output of the layer to a custom
layer that does not inherit from the nnet.layer.Formattable
class, or a
FunctionLayer
object with the Formattable
property
set to 0
(false), then the layer receives an unformatted
dlarray
object with dimensions ordered corresponding to the formats in
this table. The formats listed here are only a subset. The layer may support additional
formats such as formats with additional "S"
(spatial) or
"U"
(unspecified) dimensions.
Each input must have data of the same format.
Input Format | Output Format |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In dlnetwork
objects, AdditionLayer
objects also support
these input and output format combinations.
Input Format | Output Format |
---|---|
|
|
|
|
|
|
|
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Version History
Introduced in R2017b
See Also
Apri esempio
Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi 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)