Is there a fix to the error "A regression layer must not be preceded by a softmax layer." ?

1 回表示 (過去 30 日間)
I am trying to import my keras network into MATLAB using the "importKerasNetwork" function, but there is an issue.
In my Keras network I use a softmax activation function before my output. When I am importing into MATLAB, I specify the 'OutputLayerType' as 'regression' since that most appropriately fits my network type.
The issue I am currently having is that when I try to import this network, I get the error "A regression layer must not be preceded by a softmax layer.". Is there any work around to this problem?

採用された回答

Divya Gaddipati
Divya Gaddipati 2019 年 12 月 5 日
The softmax layer normalizes the input to the layer such that its elements sum up to 1. Therefore, it is useful when you want the network to compute a probability of a classification problem, since it ensures that the sum of the scores over the classes is 1, as requested for a probability measure.
Currently, softmaxLayer cannot directly precede a regressionLayer.
A workaround would be to define a custom softmax layer and a custom regression output layer and replace the softmax layer and output layer from the exported network.
For instructions and an example on how to define a custom regression output layer, please view the documentation page linked here:
For instructions and an example on how to define a custom layer (for softmax) with learnable parameters, please refer to the documentation page linked here:
Note: Both regression and softmax layers should be custom layers.
Firstly, import the Keras network layers along with their weights using the importKerasLayers function.
Remove the last two layers, i.e, the softmax and output layer from the network using removeLayers.
Add your custom layers to the network using addLayers.
Connect the added layers in the required manner using connectLayers.
Here’s a rough sketch of the code for your reference:
modelfile = 'your_keras_model.h5';
net = importKerasLayers(modelfile, 'ImportWeights', true);
custom_softmax_layer = mySoftmaxLayer('custom_softmax');
custom_reg_layer = myRegressionLayer('reg_out');
net = removeLayers(net, 'output');
net = removeLayers(net, 'softmax_activation');
net = addLayers(net, custom_softmax_layer);
net = addLayers(net, custom_reg_layer);
net = connectLayers(net, 'layer_before_softmax', 'custom_softmax');
net = connectLayers(net, 'custom_softmax', 'reg_out');
For more information on importKerasLayers, removeLayers, addLayers, connectLayers functions, please refer to the following links:
Hope this helps!
  1 件のコメント
Cyrus WaChong
Cyrus WaChong 2019 年 12 月 5 日
Wow, I am blown away by your thoroughness, thank you so much Divya! You are a bona fide life saver

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by