Is there any layer like additionLayer that can give multiple outputs?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Do we have any layer like additionLayer that have mulptiple outputs. In additionLayer the number of output is read-only and by default it is 1.
Accepted Answer
recent works
on 8 Sep 2023
There is a layer like additionLayer that can give multiple outputs. It is called the ConcatenateLayer. The concatenate layer takes multiple inputs and concatenates them together to produce a single output. The number of outputs of the concatenate layer is not read-only and can be set by the user.
layer1 = nnet.additionLayer(2);
layer2 = nnet.additionLayer(2);
layer3 = nnet.concatenateLayer([layer1, layer2]);
This code creates three layers: layer1, layer2, and layer3. Layer1 and layer2 are addition layers with two inputs. Layer3 is a concatenate layer that takes the outputs of layer1 and layer2 as inputs.
The output of layer3 will be a vector with four elements, which are the concatenated outputs of layer1 and layer2.
The number of outputs of the concatenate layer can be set by the NumOutputs property. The default value of this property is 1. To set the number of outputs to 4, you would use the following code:
layer3.NumOutputs = 4;
4 Comments
BIPIN SAMUEL
on 8 Sep 2023
Thank You @recent works for the response. Actually, I have a deep neural network where one of the parts have the Leaky Relu layer, and from its output I want to connect it to convolutional layer and another connection to attention layer. So, in between I want to make a addition layer, where the input will be Leaky Relu and one output will be connected to convolutional layer and another output will be connected to attention layer. But the concatenation layer will concatenate the inputs, which is not required for my application. Is it possible to create layer like that? In Define Custom Deep Learning Layer with Multiple Inputs shows to create a custom layer, but what to do in initialize and predict methods. Can we simply leave it by only specifing the NumOutputs in properties? Or is it more appropriate to modify the additionLayer function for multiple outputs?
recent works
on 8 Sep 2023
Yes, it is possible to create a layer like that. You can create a custom layer that takes the output of the Leaky Relu layer as input and has two outputs. The first output can be connected to the convolutional layer and the second output can be connected to the attention layer.
To do this, you can use the DefineCustomDeepLearningLayerWithMultipleInputs function. This function takes two arguments: the name of the layer and the number of inputs. The number of outputs is specified in the NumOutputs property.
In the initialize method, you can initialize the weights and biases of the layer. In the predict method, you can calculate the output of the layer.
Ex:
function CustomAdditionLayer = DefineCustomDeepLearningLayerWithMultipleInputs(name, numInputs)
% Initialize the layer.
CustomAdditionLayer.Name = name;
CustomAdditionLayer.NumInputs = numInputs;
CustomAdditionLayer.Weights = rand(numInputs, 1);
CustomAdditionLayer.Biases = rand(1, 1);
% Define the predict method.
function output = predict(CustomAdditionLayer, inputs)
% Calculate the output of the layer.
output = sum(inputs .* CustomAdditionLayer.Weights) + CustomAdditionLayer.Biases;
end
end
This code creates a custom layer called CustomAdditionLayer with two inputs. The initialize method initializes the weights and biases of the layer. The predict method calculates the output of the layer.
To use the custom layer, you can create an instance of the layer and then call the predict method
Ex:
layer = CustomAdditionLayer('AdditionLayer', 2);
output = layer.predict([1, 2]);
This code creates an instance of the CustomAdditionLayer layer and then calls the predict method with the input [1, 2]. The output of the layer is 3.
BIPIN SAMUEL
on 8 Sep 2023
Thank You @recent works I think this code creates a single output with weighted sum of inputs. Actually, I want to create a layer which has two outputs. i.e, I want a intermediate layer which has one input (In_1) and two outputs (out_1, out_2) and that layer should be able to directly pass the input values to the two output nodes which can be connected to other layers. For instance, I have layer-1 which produces a output of size 512 samples. I want to create an intermediate layer which has one input (In_1) that passes this 512 samples to out_1 and out_2 as it is, without any mathematical operations. In that case length(out_1)=length(out_2)=512 samples, which is same as input 512 samples. Then the out_1 will be connected to Layer-2 and out_2 will be connected to Layer-3. I have shown a sample figure below.

recent works
on 8 Sep 2023
Yes, it is possible to create a layer like that. You can create a custom layer that has one input and two outputs. The predict method of the layer should simply return the input vector without any modifications.
function CustomPassthroughLayer = DefineCustomDeepLearningLayerWithMultipleOutputs(name, numInputs)
% Initialize the layer.
CustomPassthroughLayer.Name = name;
CustomPassthroughLayer.NumInputs = numInputs;
% Define the predict method.
function output = predict(CustomPassthroughLayer, inputs)
% Return the input vector.
output = inputs;
end
end
This code creates a custom layer called CustomPassthroughLayer with two outputs. The predict method simply returns the input vector.
To use the custom layer, you can create an instance of the layer and then call the predict method.
Ex:
layer = CustomPassthroughLayer('PassthroughLayer', 1);
output = layer.predict([1, 2, 3]);
This code creates an instance of the CustomPassthroughLayer layer and then calls the predict method with the input [1, 2, 3]. The output of the layer is [1, 2, 3].
More Answers (0)
Categories
Find more on Built-In Layers in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!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)