将有状态LSTM称为功能模型? [英] Calling a stateful LSTM as a functional model?
本文介绍了将有状态LSTM称为功能模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个定义为顺序模型的有状态LSTM:
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?
在input_2
上再次应用model
时,是否保留了input_1
中的状态?如果是,如何在两次调用之间重置模型状态?
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?
推荐答案
以下有关在RNN中使用状态的说明,来自此实施Keras 如果满足以下条件,则答案为是:
Following Note on using statefulness in RNNs from this link and Keras implementation the answer is yes if:
- 两个模型中的
batch_size
是相同的(由于Keras计算内部状态的方式,这一点很重要). - 您将首先构建和编译两个模型,然后使用它们-出于某种原因,Keras正在重置图层的
build
期间的内部状态(可以检查
- 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 forreset_states
method).
如果要重置状态,可以在每个要重置状态的循环层上调用reset_states
方法.
If you want to reset states you could call reset_states
method on each recurrent layer you want ot reset states on.
这篇关于将有状态LSTM称为功能模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文