Python Keras LSTM如何在每个时间步使用隐藏状态将输入更改为LSTM的下一个时间步?
我正在使用Python Keras LSTM如何在每个时间步使用隐藏状态将输入更改为LSTM的下一个时间步?,python,tensorflow,keras,lstm,recurrent-neural-network,Python,Tensorflow,Keras,Lstm,Recurrent Neural Network,我正在使用keras构建LSTM模型。在将隐藏状态反馈回内存单元之前,我希望在每个时间步操作隐藏状态,就像正常的LSTM一样。我不确定我的实现是否正确。我发现在keras很难做到这一点 我所做的是在时间窗口的数量上循环,并独立地为特定的时间窗口提供每个输入,这样我只会在该特定输入处获得隐藏状态,并且在操纵后,将能够将其反馈给LSTM,以便下次时间窗口输入: # initialize hidden state (encoder_state_h, encoder_state_c) encoder =
keras
构建LSTM
模型。在将隐藏状态反馈回内存单元之前,我希望在每个时间步操作隐藏状态,就像正常的LSTM一样。我不确定我的实现是否正确。我发现在keras很难做到这一点
我所做的是在时间窗口的数量上循环,并独立地为特定的时间窗口提供每个输入,这样我只会在该特定输入处获得隐藏状态,并且在操纵后,将能够将其反馈给LSTM,以便下次时间窗口输入:
# initialize hidden state (encoder_state_h, encoder_state_c)
encoder = LSTM(units=units, return_state=True)
for i in range(config.time_window):
# get only the input at this time step
current_input = inputs[:, i, :]
# do some manipulations to the current input which involves the hidden states
output, encoder_state_h, encoder_state_c = encoder(current_input)
# Saving each hidden state
encoder_hidden_states.append(encoder_state_h)
# Stacking them as if normal lstm with return_sequence=True
encoder_hidden_states = tf.stack(encoder_hidden_states, axis=-1)
对每个输入所做的操作包括一个注意机制,该机制对于当前问题来说并不重要
问题是我不确定实施是否正确。我没有在上一个隐藏状态下输入到下一个时间步骤(仅输入)。Keras看起来不支持将其作为输入
是Keras自动传播隐藏状态,还是每次调用编码器时都会重置隐藏状态?
一、 本质上,我们希望在每个时间步对lstm有更多的控制,而这看起来不像是由Keras支持的
欢迎任何解决办法