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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
John Smith
John Smith il 22 Nov 2022
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?

Accedi per commentare.

Risposta accettata

John Smith
John Smith il 29 Nov 2022
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.

Più risposte (0)

Categorie

Scopri di più su Image Data Workflows in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by