Keras 的 LSTM 的时间步长是多少?

mac*_*ccN 3 python lstm keras tensorflow

我在 Keras 中的 LSTM 实现遇到了一些麻烦。

我的训练集结构如下:

  • 序列数:5358
  • 每个序列的长度为 300
  • 序列的每个元素都是一个包含 54 个特征的向量

我不确定如何为有状态 LSTM 塑造输入。

按照本教程:http : //philipperemy.github.io/keras-stateful-lstm/,我创建了子序列(在我的例子中,有 1452018 个子序列,window_size = 30)。

为有状态 LSTM 的输入重塑数据的最佳选择是什么?

在这种情况下,输入的时间步长是什么意思?为什么?

batch_size 与时间步长有关吗?

Ami*_*mir 6

我不确定如何为有状态 LSTM 塑造输入。

LSTM(100, statefull=True)
Run Code Online (Sandbox Code Playgroud)

但是在使用有状态 LSTM 之前问问自己我真的需要statefullLSTM 吗?有关更多详细信息,请参阅此处此处

为有状态 LSTM 的输入重塑数据的最佳选择是什么?

这真的取决于手上的问题。但是,我认为您不需要重新整形,只需将数据直接输入 Keras:

input_layer = Input(shape=(300, 54))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,输入的时间步长是什么意思?为什么?

在您的示例中,时间戳是 300。有关时间戳的更多详细信息,请参见此处。在下图中,我们将 5 个时间戳输入到 LSTM 网络中。

在此处输入图片说明

batch_size 与时间步长有关吗?

不,它与batch_size无关。可以在此处找到有关 batch_size 的更多详细信息。


这是基于您提供的描述的简单代码。它可能会给你一些直觉:

import numpy as np
from tensorflow.python.keras import Input, Model
from tensorflow.python.keras.layers import LSTM
from tensorflow.python.layers.core import Dense

x_train = np.zeros(shape=(5358, 300, 54))
y_train = np.zeros(shape=(5358, 1))

input_layer = Input(shape=(300, 54))
lstm = LSTM(100)(input_layer)
dense1 = Dense(20, activation='relu')(lstm)
dense2 = Dense(1, activation='sigmoid')(dense1)

model = Model(inputs=input_layer, ouputs=dense2)
model.compile("adam", loss='binary_crossentropy')
model.fit(x_train, y_train, batch_size=512)
Run Code Online (Sandbox Code Playgroud)