4
$\begingroup$

I watched the this video on YouTube and copied the code. Why it does not work in M12? I mean net[inData] and rnnLayerForward[param, inData, state] don't give the same result? What change since then and how can I fix it? Slides can be downloaded here.

    rnnTimeStep[params_Association, input_, state_] := 
      Tanh[params["StateWeights"].state + params["InputWeights"].input + 
        params["Biases"]];
    rnnLayerForward[params_Association, input_, state_] := 
     Rest@FoldList[rnnTimeStep[params, #2, #1] &, state, input]

sequenceLength = 3;
featureSize = 2;
rnnStateSize = 1;
inData = RandomReal[1, {sequenceLength, featureSize}];
state = ConstantArray[0, rnnStateSize];

net = NetInitialize@
  BasicRecurrentLayer[rnnStateSize, "Input" -> Dimensions@inData]

param = Normal@NetExtract[net, "Arrays"]

net[inData]

rnnLayerForward[param, inData, state]
$\endgroup$
2
  • $\begingroup$ param = Normal[#] & /@ NetExtract[net, "Arrays"] $\endgroup$ Commented Jul 26, 2019 at 7:00
  • $\begingroup$ Thanks. Can you please post as an answer, so I can accept it. param = Normal/@ NetExtract[net, "Arrays"] is also fine. $\endgroup$ Commented Jul 26, 2019 at 13:48

1 Answer 1

4
$\begingroup$
rnnTimeStep[params_Association, input_, state_] := 
  Tanh[params["StateWeights"].state + params["InputWeights"].input + params["Biases"]];

rnnLayerForward[params_Association, input_, state_] := 
 Rest@FoldList[rnnTimeStep[params, #2, #1] &, state, input]

sequenceLength = 3;
featureSize = 2;
rnnStateSize = 1;
inData = RandomReal[1, {sequenceLength, featureSize}];
state = ConstantArray[0, rnnStateSize];

net = NetInitialize@BasicRecurrentLayer[rnnStateSize, "Input" -> Dimensions@inData];

param = Normal[#] & /@ NetExtract[net, "Arrays"];
net[inData]

{{-0.85818}, {-0.987016}, {-0.941039}}

rnnLayerForward[param, inData, state]

{{-0.85818}, {-0.987016}, {-0.941039}}

$\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.