Call stateful LSTM as a functional model?

I have a state-based LSTM defined as a sequential model:

model = Sequential() model.add(LSTM(..., stateful=True)) ... 

Later I use it as a functional model:

 input_1, input_2 = Input(...), Input(...) output_1 = model(input_1) output_2 = model(input_2) # Is the state from input_1 preserved? 

Is the state from input_1 when we apply model again on input_2 ? If so, how can I reset the model state between calls?

+7
python machine-learning neural-network keras recurrent-neural-network
source share
1 answer

Following the note on using state in RNN from this link and Keras implementation the answer is yes if:

  • batch_size is the same in both models (this is important because of how Keras calculates internal states).
  • First you created and compiled both models, and then used them - for some reason Keras restores internal states during the build layer (you can check it here by looking for the reset_states method).

If you want to reset states, you can call the reset_states method at each repeating level at which you want to enable reset.

0
source share

All Articles