Cannot have a dimension-increasing custom layer with dlnetwork

12 ビュー (過去 30 日間)
John Smith
John Smith 2019 年 12 月 13 日
編集済み: John Smith 2021 年 10 月 5 日
Hello,
I implemented in 2019b a simple reshape layer for Deep Learning which I'd like to use with a dlnetwork.
The network passes analyzeNetwork(), but fails with:
Error using dlarray (line 179)
Number of dimension labels must be greater than or equal to the number of dimensions in the input array.
Error in nnet.internal.cnn.layer.util.CustomLayerFunctionalStrategy>@(zi)dlarray(zi,Xdims)
After some digging, turns out the problem is in R2019b\toolbox\nnet\cnn\+nnet\+internal\+cnn\+layer\+util\CustomLayerFunctionalStrategy.m in predict() and forward() functions. There, the labels of the input X are captured through
[expectedType, Xdims, X] = processInput(this, X);
and then the labels are reapplied to the output of the layer in
Z = cellfun(@(zi) dlarray(zi, Xdims), Z, 'UniformOutput', false);
When the custom layer increases the number of dimensions of the output (as e.g. in reshape()) the error occurs.
Please report this bug/defficiency to Mathworks and suggest a workaround.
Thx,
D
PS This behaviour may exist in other places too.
PSS BTW, note that both predict() and forward() expect the output to be an ulabeled dlarray and verify this through calls to
this.LayerVerifier.verifyUnlabeledDlarray( 'predict', layer.OutputNames, Z )
or
this.LayerVerifier.verifyUnlabeledDlarray( 'forward', layer.OutputNames, Z )
so I'm not sure how the extra labels could be passed.

採用された回答

Katja Mogalle
Katja Mogalle 2021 年 7 月 1 日
Hi John,
The capability that you are asking for has been added in R2021a. The following documentation example shows how to implement a custom layer that can change the number of data dimensions: https://www.mathworks.com/help/deeplearning/ug/define-custom-deep-learning-layer-with-formatted-inputs.html
A workaround could be to split the dlnetwork into two dlnetworks, one for the part before the reshape layer and one for the part after the reshape layer. Then you can do the reshaping of the output dlarrays from the first network in plain MATLAB code and pass the reshaped data to the second network.
I hope this helps.
Katja
  3 件のコメント
Jose Cortes-Briones
Jose Cortes-Briones 2021 年 10 月 4 日
Hi John, Yes, this is a pain. My reshape layers stopped working when I tried to use them with dlnetworks. What I did was to add dummy singleton dimensions (labels) to the dlarrays from the start. These dimensions are just a waste of space/memory but the dlnetwork won't complain about a change in the number of labels when you reshape the dlarray.
John Smith
John Smith 2021 年 10 月 5 日
編集済み: John Smith 2021 年 10 月 5 日
Turns out they DID provided this capability in R2021a. You just need to inherit your custom layer ALSO from nnet.layer.Formattable. Then all inputs to the layer are formatted (have labels) and the outputs also must be formatted (upto your code).
Works great!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCustom Training Loops についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by