How to combine the states of a custom stateful layer with a stateful nested sub-network?

2 ビュー (過去 30 日間)
I have a stateful custom layer with a statful nested sub-network.
In my predict method I call predict on the sub-network and obtain the resulting state. Then I do some computation and obtain the state of the custom layer. Now, I'm supposed to return the state of the layer and assign it back to net.State property.
But I have two states - that of the layer and that of the sub-network. How to handle this situation?
  2 件のコメント
John Smith
John Smith 2022 年 11 月 22 日
This is different. There're examples how to have a layer with multiple state properties. I have a layer and a nested sub-network, both are stateful. So one state property belongs to the layer while the other belongs to the sub-network.
So, say I have in the predict method the following code snippet:
function [Z, state] = predict(X)
[Zsub, statesub] = predict(X, layer.Subnetwork);
[Z, statelayer] = layercalc(X, Zsub);
state = ????
How should state be set?



John Smith
John Smith 2022 年 11 月 29 日
OK, I figured this out by implementing and debugging such a setup:
First, there are two predict (and forward) methods - one for the dlnetwork object and another for your custom layer.
The dlnetwork's method returns a State which is a table with one row per each layer's state variable.
Behind the scenes, both built-in and custom stateful layers have a State dependent propery, which getter function maps from the table to state variables and which setter maps from state variables to a table.
It is CRUCIAL that the order of the state variables in properties definitions matches the order of states returned in predict and forward functions.
When network's predict runs, the states returned by each layer are concatenated into a single State table.
So, in your predict method return your layer's state variables and the State returned by predict invoked on the sub-network.
PS You also need to implement the initialize method and initialize your state variables in it. Also, you have to call initialize on your sub-network or it won't initialize.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by