I have a stateful 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
preserved when we apply model
again on input_2
? If yes, how can I reset the model state in between the calls?
Following Note on using statefulness in RNNs from this link and Keras implementation the answer is yes if:
- The
batch_size
in both models is the same (it's important due to the way Keras computes the inner states).
- You would first build and compile both models and then use them - for some reason Keras is resetting the inner states during the
build
of a layer (you can check it here by looking for reset_states
method).
If you want to reset states you could call reset_states
method on each recurrent layer you want ot reset states on.