Keras LSTM 多维输入

Raj*_*ath 3 machine-learning neural-network theano keras recurrent-neural-network

我的输入时间序列数据的形状为 (nb_samples, 75, 32)。
75 是时间步长,32 是输入维度。

model = Sequential()
model.add(LSTM(4, input_shape=(75, 32)))
model.summary()
Run Code Online (Sandbox Code Playgroud)

LSTM 权重向量[W_i, W_c, W_f, W_o]都是 32 维,但输出只是一个值。上述模型的输出形状为(1,4)。但在 LSTM 中,输出也是一个向量,所以对于上面的多对一实现来说,它不应该是 (32,4) 吗?为什么它也为多维输入提供单个值?

Nas*_*Ben 5

正如您可以在Keras 文档中阅读的重复层一样

对于 shape 的输入(nb_sample, timestep, input_dim),您有两种可能的输出:

  • 如果您在 LSTM 中设置return_sequence=True(这不是您的情况),您将返回每个隐藏状态,因此 LSTM“读取”您的序列时的中间步骤。您将得到 shape 的输出(nb_sample, timestep, output_dim)

  • 如果您设置return_sequence=False(这是默认值),它将只输出最后的状态。所以你会得到 shape 的输出(nb_sample, output_dim)

因此,如果您像这样定义 LSTM 层:

model.add(LSTM(4, return_sequence=True, input_shape=(75, 32)))
Run Code Online (Sandbox Code Playgroud)

你将得到 shape 的输出(None, 75, 4)。如果您的时间维度为 32,则必须在将数据输入 LSTM 之前对其进行转置。第一个维度是时间维度。

我希望这有帮助 :)