Clear Filters
Clear Filters

Is there any layer like additionLayer that can give multiple outputs?

11 views (last 30 days)
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
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
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
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].

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!