3
$\begingroup$

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]

enter image description here

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[%%%%, %%%, %%, %]|>]

enter image description here

...

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}]

enter image description here

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?

$\endgroup$
4
  • 1
    $\begingroup$ Nice question, looks like a task for SequenceFold, surprised there's no answer yet. +1 $\endgroup$
    – LLlAMnYP
    Commented Jun 22, 2017 at 14:52
  • $\begingroup$ @LLlAMnYP Nice to hear that :) $\endgroup$
    – partida
    Commented Jun 22, 2017 at 15:12
  • $\begingroup$ Self-answers are encouraged here. If you think you've achieved your goals, why not transfer your update to a proper answer? $\endgroup$
    – LLlAMnYP
    Commented Jun 23, 2017 at 8:04
  • $\begingroup$ @LLlAMnYP right ,thank you $\endgroup$
    – partida
    Commented Jun 23, 2017 at 11:01

1 Answer 1

1
$\begingroup$

Thanks LLlAMnYP,I try SequenceFoldList and find it can generate interesting result.

SequenceFoldList[f, {0, 0, 0, 0}, Range[5], 4] // Column

enter image description here

So I am sure it can be used in here.

Module[{i = 1}, Drop[#, 4] &@
   SequenceFoldList[
     net[<|"Input1" -> data[[i++]], 
           "history" -> Flatten@{##}|>] &, 
     {ConstantArray[0, 8], ConstantArray[0, 8], ConstantArray[0, 8],ConstantArray[0, 8]}, 
     Range[Length@data-1], 4]] // MatrixForm

enter image description here

It got a correct result.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.