Calling a stateful LSTM as a functional model?

感情迁移 提交于 2019-12-14 00:49: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:


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.



来源:https://stackoverflow.com/questions/41759332/calling-a-stateful-lstm-as-a-functional-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!