keras - Keras 中 LSTM 的数学公式?

标签 keras lstm keras-layer

根据 wikipedia-lstm-math-equation 中的数学公式, 如下所示, LSTM Equation

应该只有隐藏状态h_t 和单元格状态c_t。但是,当我尝试在 Keras 上编写 RNN 代码时,存在三个:lstm_outputstate_hstate_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>)

最佳答案

让我们分解一下,看看this line来自源代码 - return h, [h, c]:

  • lstm_output:是每个时间步长的h。所以它的形状是 (batch_size, sequence_length, hidden_​​size),在你的例子中是 (?, 28, 32)。作为documentation说,它作为一个序列返回,因为你设置了 return_sequences=True
  • state_h:last 时间步长的h,如果您可以检查,它应该等于lstm_output[:,- 1]。请注意为什么它的形状是 (?, 32),因为它是最后一个时间步的输出,而不是每个时间步的输出。
  • state_c:最后时间步长的 c

这些方程通常以不同的方式实现以优化某些特征,但它们都遵循 original paper .请注意,激活可能会有变化,例如使用 hard_sigmoid 进行循环激活,这些应该在文档中明确注明。

关于keras - Keras 中 LSTM 的数学公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809242/

相关文章:

machine-learning - LSTM 张量形状和超参数 Tensorflow

python - keras 理解词嵌入层

python - Keras 中的 3D 卷积是否适用于 RGB 视频?

tensorflow - 如何在 Keras 中包装自定义 TensorFlow 损失函数?

python - 训练深度学习模型时如何处理大型csv文件?

python - 我想知道我关于使用 keras 实现 lstm 层是否正确

tensorflow - Tensorflow中可变长度序列的双向LSTM

Keras weight scaling-计算图

python - 如何在 Keras 中获取图层的权重?

python - 如何从预训练模型中删除最后一层。我尝试过 model.layers.pop() 但它不起作用