How to automatically initialize learnable parameters of a custom layer?

3 ビュー (過去 30 日間)
John Smith
John Smith 2021 年 10 月 5 日
回答済み: John Smith 2022 年 11 月 10 日
Matlab DOES propagate configuration information to a custom layer when it automatically initializes learnable parameters, but NOT to the layer implementation.
Specifically, there's the configureForInputs() method in the Layer and CustomLayer classes which are invoked at the beginning of automatic learnable parameters initialization so that the layer can configure itself for input-dependent information (e.g., number of channels).
But while Layer propagates the call to inferSize() method of the layer implementation class (e.g., Convolution2D), CustomLayer does NOT do this (it calls setLearnablesToAutoInitialize() which sets the nested dlnetworks to auto-initialize but does NOT propagate the call to the layer implementation, i.e. user's code).
In addtion, there's another method in user's layer implementation of automatic initialization that isn't propagated to user's code by CustomLayer - initializeLearnableParameters():
initializeLearnableParameters() calls initialize() on any dlnet nested inside the custom layer, but doesn't propagate the call to user's implementation.
Note that only these two methods can modify the layer since only they return a possibly modified 'this' object.
PS A kludgy workaround is to define a dlnetwork and then to infer the configuration parameters therefrom, but it's a kludge and it wastes a lot of memory!

採用された回答

John Smith
John Smith 2022 年 11 月 10 日
It looks that Mathworks has added an option to initialize custom layers at network initialization time in R2022b.
There's now the initialize() method that the custom layer can provide and which will be called upon network initialization with a networkDataLayout object that describes the input. Furthermore, this method can modify the layer and return the modified layer to the caller for inclusion in the network.
See https://www.mathworks.com/help/deeplearning/ug/define-custom-deep-learning-layer.html

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by