根据wikipedia-lstm-math-equation中的数学公式,如下所示,应该只有隐藏状态
我现在想知道
h_t
和细胞状态c_t
。然而,当我试图在Keras上编写RNN代码时,有三个变量:lstm_output
、state_h
和state_c
。我现在想知道
lstm_output
的数学公式是什么?
以下是我的代码:from keras.layers import Input, LSTM
lstm_input = Input(shape=(28, 10))
lstm_output, state_h, state_c = LSTM(units=32,
return_sequences=True,
return_state=True,
unroll=True)(lstm_input)
print(lstm_output, state_h, state_c)
并且它给出了
Using TensorFlow backend.
(<tf.Tensor 'lstm_1/transpose_1:0' shape=(?, 28, 32) dtype=float32>, <tf.Tensor 'lstm_1/mul_167:0' shape=(?, 32) dtype=float32>, <tf.Tensor 'lstm_1/add_221:0' shape=(?, 32) dtype=float32>)
return_sequences
,该层的输出总是会被返回的。这是每个层都会做的事情。return_sequences
将定义输出是否包含整个序列或仅最后一步。 - Daniel Möllerreturn_sequences=True, return_state=True
,它会给出lstm_output
(3D)、state_h
(2D)、state_c
(2D),而且lstm_output[:, -1, :]
等于state_h
。其次,使用return_sequences=False, return_state=True
,它会给出lstm_output
(2D)、state_h
(2D)、state_c
(2D),而且lstm_output
等于state_h
。 - guorui