Suppose I have a net
SeedRandom[1234];
net = NetInitialize@NetGraph[{ReshapeLayer[{4, 8}],
BasicRecurrentLayer[8], SequenceLastLayer[],
CatenateLayer[], 8, Ramp, 8},
{NetPort["history"] -> 1 -> 2 -> 3 -> 4,
NetPort["Input1"] -> 4 -> 5 -> 6 -> 7},
"history" -> 8*4,
"Input1" -> 40]
This is the Input1
,it has 20 examples.
data = RandomReal[1, {20, 40}];
Because of the output of this net has 8-dims Output
.
Four past outputs constitute the 32-dims vector and the net use this vector and Input1
to predict next history
.
Assuming at time 1,there is no history and history
is all zero.
time 1:
net[<|"Input1" -> data[[1]], "history" -> ConstantArray[0, 8*4]|>]
time 2:
net[<|"Input1" -> data[[2]], "history" -> Join[ConstantArray[0, 8*3], %]|>]
time 3:
net[<|"Input1" -> data[[3]], "history" -> Join[ConstantArray[0, 8*2], %%, %]|>]
time 4:
net[<|"Input1" -> data[[4]], "history" -> Join[ConstantArray[0, 8*1], %%%, %%, %]|>]
time 5:
net[<|"Input1" -> data[[5]], "history" -> Join[%%%%, %%%, %%, %]|>]
time 6:
net[<|"Input1" -> data[[6]], "history" -> Join[%%%%, %%%, %%, %]|>]
...
time 20:
net[<|"Input1" -> data[[20]], "history" -> Join[%%%%, %%%, %%, %]|>]
I try this code but it can not be combined with other codes well.
And it mismatch the result at time 6
out = ConstantArray[0, {20, 8}];
out[[1]] = net[<|"Input1" -> data[[1]], "history" -> ConstantArray[0, 8*4]|>];
out[[2]] = net[<|"Input1" -> data[[2]], "history" -> Join[ConstantArray[0, 8*3], %]|>];
out[[3]] = net[<|"Input1" -> data[[3]], "history" -> Join[ConstantArray[0, 8*2], %%, %]|>];
out[[4]] = net[<|"Input1" -> data[[4]], "history" -> Join[ConstantArray[0, 8*1], %%%, %%, %]|>];
Do[out[[i]] = net[<|"Input1" -> data[[i]],
"history" -> Join[%%%%, %%%, %%, %]|>], {i, 5, 20}]
So is there a nice way or a functional style to implement this and got the Output
from time 1 to 20?
How can trace the recent four historical results,FoldList
can do it?