Calling a stateful LSTM as a functional model?

2019-06-18 04:41发布

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?

1条回答
【Aperson】
2楼-- · 2019-06-18 05:17

Following Note on using statefulness in RNNs from this link and Keras implementation the answer is yes if:

  1. The batch_size in both models is the same (it's important due to the way Keras computes the inner states).
  2. 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.

查看更多
登录 后发表回答