필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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
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 = ????
end
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개)

카테고리

Help CenterFile Exchange에서 Statistics 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