0

在 Keras 中运行我的 LSTM 时出现以下错误:

ValueError: If a RNN is stateful, it needs to know its batch size. Specify the batch size of your input tensors: 
- If using a Sequential model, specify the batch size by passing a `batch_input_shape` argument to your first layer.
- If using the functional API, specify the batch size by passing a `batch_shape` argument to your Input layer.

我有一个 Pandas 数据框,它是一个按分钟索引的时间序列,训练集和测试集为X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, shuffle = False). X_train.shape是 (27932, 7) 并且X_test.shape是 (6984, 7)。我将 X_train 和 X_test 归一化并重塑为 3D 为:

# Normalizing the data
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

X_train = X_train.reshape(6983, 4, X_train.shape[1])
X_test = X_test.reshape(1746, 4, X_test.shape[1])
y_train = y_train.values.reshape(6983, 4, 1)
y_test = y_test.values.reshape(1746, 4, 1)

X 重塑背后的逻辑是我希望我的 LSTM 在 6983 个样本中学习 4 个时间步长(即 4 分钟)的样本。我想针对(X_train.shape[0], 1, X_train.shape[1]).

我的 LSTM 如下:

# Creating our model's structure
model = Sequential()
model.add(Bidirectional(LSTM(4, batch_input_shape = (X_train.shape[0], X_train.shape[1], X_train.shape[2]), return_sequences = True, stateful = True)))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(4)))
model.add(Dense(1, activation = 'sigmoid'))
es = EarlyStopping(monitor = 'val_loss', patience = 10)

# Compiling the model
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['Recall'])

# Fitting the model
history = model.fit(X_train, y_train, epochs = 50, verbose = 1, validation_data = (X_test, y_test), callbacks = [es])

具有讽刺意味的是,即使我在 LSTM 的第一层中明确说明batch_input_shapeand ,我也有上述错误。如果它使用 X_train 的 3D 形状和 X_test 的 3D 形状,stateful = True我运行我的 LSTM 没有问题。(X_train.shape[0], 1, X_train.shape[1])(X_test.shape[0], 1, X_test.shape[1])

我的代码的哪一部分触发了错误?

顺便说一句,我无法使用比我在代码中描述的更多的隐藏单元来提高 LSTM 的性能。这看起来令人惊讶吗?

4

0 回答 0