Multiple softmax vectors in output layer of neural network using softmaxLayer

7 views (last 30 days)
I'm using deep learning toolbox in MATLAB 2021a. And the neural network that I'm trying to build has multiple softmax vectors in output layer. (e.g. 10 softmax vectors of length 8). That is, the calculation is similar to how in-built softmax() function applies to each column of a matrix.
e.g.
>> a = randn(2,2)
a =
-1.1803 0.2963
1.6926 -0.1352
>> softmax(a)
ans =
0.0535 0.6062
0.9465 0.3938
However, I couldn't find a way to do this with softmaxLayer.
My code looks like this.
layersDNN = [
featureInputLayer(numInputs, 'Name', 'in')
fullyConnectedLayer(numInputs*2, 'Name', 'fc1')
batchNormalizationLayer('Name', 'bn1')
reluLayer('Name', 'relu1')
fullyConnectedLayer(numInputs*8, 'Name', 'fc2')
softmaxLayer('Name', 'sm1')
];
I'm trying to get the softmaxLayer to divide numInputs*8 nodes in last layer to numInputs vectors of length 8 and apply softmax function separately.
Alternatively I'm trying to remove softmaxLayer and apply softmax to reshaped output of network. Something like this.
lgraphDNN = layerGraph(layersDNN);
dlnetDNN = dlnetwork(lgraphDNN);
out1 = forward(dlnetDNN, X);
out2 = reshape(out1, [numInputs, 8]);
pred = softmax(out2);
% calculate loss, gradients etc.
I'm not sure if this is a good solution. I'd like to know if there's a way to do this using softmaxLayer, since the requirement doesn't feel like an extreme case.
  2 Comments
Isuru Rathnayaka
Isuru Rathnayaka on 12 Dec 2021
Edited: Isuru Rathnayaka on 12 Dec 2021
Hi Abolfazl, I'd like to know if this is doable using softmaxLayer. My idea seems like a bit of a hack and the requirement doesn't feel strange enough to resort to a hack. I rephrased the end of the question to make my question clear.
As for the alternative, it doesn't give any errors. However, I couldn't verify if it works yet because of another issue in my code. I'm not very familiar with Matlab and deep learning toolbox.

Sign in to comment.

Accepted Answer

Prachi Kulkarni
Prachi Kulkarni on 10 Jan 2022
Edited: Prachi Kulkarni on 12 Jan 2022
Hi,
From the R2021b release onwards, you can create numInputs number of fully connected layers, each with output size 8. Every fully connected layer can then be connected to its own softmax layer.
The outputs from the softmax layers can be concatenated using a concatenation layer and then passed on to the output layer.
For more information, see the documentation for Concatenation layer.
  2 Comments
Isuru Rathnayaka
Isuru Rathnayaka on 12 Jan 2022
Hi Prachi,
Thanks very much for the detailed answer. I didn't know about the concatenation layer. I'll try this method.
Christian Holz
Christian Holz on 14 Jun 2023
Hello all,
a question regarding the proposed solution: were you able to implement it? Can you show the details?
Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!