如何为 keras 中的 LSTM 回归准备输入数据?

How to prepare input data for a LSTM regression in keras?

我有一个包含 5K 行(-1K 用于验证)和 17 列的数据集,包括最后一列(目标整数二进制标签)。

我的模型就是这个 2 层 LSTM:

model = Sequential()
model.add(Embedding(output_dim=64, input_dim=17))
model.add(LSTM(32, return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(32, return_sequences=False))
model.add(Dense(1))

model.compile(loss='binary_crossentropy', optimizer='rmsprop',
              class_mode='binary')

使用 pandas

加载我的数据集后
df_train = pd.read_csv(train_file)
train_X, train_y = df_train.values[:, :-1], df_train['target'].values

并尝试 运行 我的模型,我收到此错误:

Exception: When using TensorFlow, you should define explicitly the number of timesteps of your sequences. - If your first layer is an Embedding, make sure to pass it an "input_length" argument. Otherwise, make sure the first layer has an "input_shape" or "batch_input_shape" argument, including the time axis.

我应该在 input_length 中输入什么?总行数?

由于我的数据框的形状为 train_X=(4000, 17) train_y=(4000,) 我该如何准备它来提供这种模型?我必须更改我的输入数据形状吗?

感谢您的帮助!! (=

看起来 Keras 使用静态展开方法在 TensorFlow 上构建循环网络(例如 LSTM)。 input_length 应该是你想要训练的最长序列的长度:所以如果你的 CSV 文件的每一行 train_file 是一个逗号分隔的符号序列,它应该是符号的数量最长的一排。