Keras 中 LSTM 的多维输入

Multi dimensional input for LSTM in Keras

我想了解 RNN,特别是 LSTM 如何使用 Keras 和 Tensorflow 处理多个输入维度。我的意思是输入形状是 (batch_size, timesteps, input_dim) where input_dim > 1.
如果 input_dim = 1.
,我认为下面的图片很好地说明了 LSTM 的概念 这是否意味着如果 input_dim > 1 那么 x 不再是单个值而是一个数组?但是如果是这样那么权重也变成了数组,形状与 x + 上下文相同?

Keras 创建了一个计算图,它根据特征(但对于所有单元)执行底部图片中的序列。这意味着状态值 C 始终是一个标量,每个单位一个。它不是一次处理特征,而是一次处理单元,然后分别处理特征。

import keras.models as kem
import keras.layers as kel

model = kem.Sequential()
lstm = kel.LSTM(units, input_shape=(timesteps, features))
model.add(lstm)
model.summary()

free_params = (4 * features * units) + (4 * units * units) + (4 * num_units)
print('free_params ', free_params)
print('kernel_c', lstm.kernel_c.shape)
print('bias_c', lstm.bias_c .shape)

其中 4 代表底部图片中的 f、i、c 和 o 内部路径中的每一个。第一项是内核的权重数,第二项是循环内核的权重,最后一项是偏差(如果应用)。对于

units = 1
timesteps = 1
features = 1

我们看到了

Layer (type)                 Output Shape              Param #
=================================================================
lstm_1 (LSTM)                (None, 1)                 12
=================================================================
Total params: 12.0
Trainable params: 12
Non-trainable params: 0.0
_________________________________________________________________
num_params 12
kernel_c (1, 1)
bias_c (1,)

units = 1
timesteps = 1
features = 2

我们看到了

Layer (type)                 Output Shape              Param #
=================================================================
lstm_1 (LSTM)                (None, 1)                 16
=================================================================
Total params: 16.0
Trainable params: 16
Non-trainable params: 0.0
_________________________________________________________________
num_params 16
kernel_c (2, 1)
bias_c (1,)

其中 bias_c 是状态 C 输出形状的代理。请注意,关于单元的内部制作有不同的实现。详情在此处 (http://deeplearning.net/tutorial/lstm.html),默认实现使用 Eq.7。希望这有帮助。

让我们将上述答案更新为 TensorFlow 2。

import tensorflow as tf

model = tf.keras.Sequential([tf.keras.layers.LSTM(units, input_shape=(timesteps, features))])
model.summary()

free_params = (4 * features * units) + (4 * units * units) + (4 * num_units)
print('free_params ', free_params)
print('kernel_c', lstm.kernel_c.shape)
print('bias_c', lstm.bias_c .shape)

使用此代码,您也可以在 TensorFlow 2.x 中获得相同的结果。