AdditionLayer - Addition layer - MATLAB (original) (raw)

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

Syntax

Description

`layer` = additionLayer(`numInputs`) creates an addition layer that adds numInputs inputs element-wise. This function also sets the NumInputs property.

example

`layer` = additionLayer(`numInputs`,'Name',name) also sets the Name property.

example

Properties

expand all

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 isNumInputs. 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.

Layer name, specified as a character vector or string scalar. For Layer array input, the trainnet anddlnetwork 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

collapse all

Create and Connect Addition Layer

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.

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

Algorithms

expand all

Layer Input and Output Formats

Layers in a layer array or neural network 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:

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 those for developing a custom layer, using a functionLayer object, or using the forward andpredict functions with dlnetwork objects.

This table shows the supported input formats ofAdditionLayer 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 theFormattable property set to 0 (false), then the layer receives an unformatteddlarray object with dimensions ordered according 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.

Input Format Output Format
"CB" (channel, batch) "CB" (channel, batch)
"SCB" (spatial, channel, batch) "SCB" (spatial, channel, batch)
"SSCB" (spatial, spatial, channel, batch) "SSCB" (spatial, spatial, channel, batch)
"SSSCB" (spatial, spatial, spatial, channel, batch) "SSSCB" (spatial, spatial, spatial, channel, batch)
"CBT" (channel, batch, time) "CBT" (channel, batch, time)
"SCBT" (spatial, channel, batch, time) "SCBT" (spatial, channel, batch, time)
"SSCBT" (spatial, spatial, channel, batch, time) "SSCBT" (spatial, spatial, channel, batch, time)
"SSSCBT" (spatial, spatial, spatial, channel, batch, time) "SSSCBT" (spatial, spatial, spatial, channel, batch, time)
"CU" (channel, unspecified) "CU" (channel, unspecified)
"SC" (spatial, channel) "SC" (spatial, channel)
"SSC" (spatial, spatial, channel) "SSC" (spatial, spatial, channel)
"SSSC" (spatial, spatial, spatial, channel) "SSSC" (spatial, spatial, spatial, channel)
"BU" (batch, unspecified) "BU" (batch, unspecified)

If several input formats contain the same label, then the corresponding data for each input must be a singleton or the same size as the nonsingleton data for the other inputs. If an input format contains multiple U or S labels,additionLayer matches each label to the labels in the other input formats by their order of appearance.

In dlnetwork objects, AdditionLayer objects also support these input and output format combinations.

Input Format Output Format
"CT" (channel, time) "CT" (channel, time)
"SCT" (spatial, channel, time) "SCT" (spatial, channel, time)
"SSCT" (spatial, spatial, channel, time) "SSCT" (spatial, spatial, channel, time)
"SSSCT" (spatial, spatial, spatial, channel, time) "SSSCT" (spatial, spatial, spatial, channel, time)

Complex Numbers

AdditionLayer objects support complex-valued input and outputs. (since R2024a) The layer applies the same underlying operation to complex-valued input as it does to real-valued input and outputs complex-valued data where applicable.

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

expand all

R2024a: Support for complex-valued inputs and outputs

AdditionLayer objects support complex-valued input and outputs. The layer applies the same underlying operation to complex-valued input as it does to real-valued input and outputs complex-valued data where applicable.