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

1 回表示 (過去 30 日間)
John Smith 2022 年 11 月 22 日

Hello,
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?
Thx,
J
##### 2 件のコメントなしを表示なしを非表示
Dhananjay Kumar 2022 年 11 月 22 日
You can have two State properties I guess ?
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 = ????
end
How should state be set?

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

### 採用された回答

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.

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

### カテゴリ

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

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by