how can I make a deep learning custom layer to take multiple inputs like addition/concatenation?

4 ビュー (過去 30 日間)
I am inplementating attention mechanism for image segmentation, one step in attention is to take dot product of the attention coefficient and input feature map,however, matlab neural network toolbox doesn't support such an action yet. What I tried was to modify the "Addition layer" and relavant functions to make a new class/functions to handle dot product of two inputs and I also added those functions to the path. It kind of works so it does generate an "attention layer" and it can take two inputs, however, when I train the network, it gets error saying Error using nnet.internal.cnn.analyzer.NetworkAnalyzer/propagateSizes (line 223): Index exceeds array bounds.
  2 件のコメント
Markus Wagner
Markus Wagner 2019 年 1 月 2 日
I have the same problem. I want implementate a custome hidden layer and a custome regression layer with 2 inputs like the addition/concatenation layer for bulid up a VAE Network.
After adapt the class of the Addition Layer and use in a layerGraph it failed by a Error:
Error using nnet.internal.cnn.util.validateLayersForLayerGraph (line 20)
Expected input to be one of these types:
nnet.cnn.layer.Layer
Instead its type was latentLayer.
Hanxx Sakura
Hanxx Sakura 2019 年 1 月 17 日
編集済み: Hanxx Sakura 2019 年 1 月 17 日
I think there is no official way to implement module with multiple inputs, but I finally accomplish it inspired by the Addition.m and AdditionLayer.m as follows:
  1. implement an internal layer (e.g., myAdd) like the "Addition" Class, such as, defining the variables, forward / backforward function;
  2. since the internal layer cannot be used in the layerGraph directly, wrap the internal layer by an external class (e.g., myAddLayer) as in "AdditionLayer.m";
  3. create an object of your module by using myAddLayer();
hope this can help~

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

回答 (1 件)

Maksym Tymchenko
Maksym Tymchenko 2023 年 3 月 10 日
Since release R2022b, MATLAB directly supports the attention mechanism that you are trying to implement.
Check the documentation of the attention function at the link below:

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by