无法在 Keras 中构建 LSTM 层

Cannot build LSTM layers in Keras

我正在尝试开发 Keras LSTM 代码。我有代表音频特征的输入数据,就是这样。

Training items [210969]

Feature vector [40]

Batch size [256]

数据示例

[[-1.66514225 -0.44892017 0.24430933 ..., -0.96550658 -1.3456034 -1.76591301] [-1.79528513 -1.68822871 0.05168475 ..., -0.23602946 -0.26985581 -0.0951985 ] [-0.09728797 -1.27619953 -1.48452426 ..., -1.11481965 -0.22158974 -1.10055417]]

在我的 Python 代码中,我使用了 Keras 库。我尝试构建 LSTM 层。我正在使用 input_shape 但我不能很好地使用。我尝试将我的输入数据从 2 维重塑为 3 维。你可以在拟合函数中看到。

.........
elif classname == 'LSTM':
    layer_setup['config']['input_shape'] = (210969,40)
............
.......... 
model.fit(
            x=X_training.reshape(256,X_training.shape[0],X_training.shape[1]),
            y=Y_training.reshape(256,Y_training.shape[0],Y_training.shape[1]),
            batch_size=256,
            epochs=500,
            validation=validationdata,
            verbose=0,
            shuffle=true,
            callbacks=callbacks,
            class_weight=class_weight
        )

我在 运行 代码

时遇到了那个错误

cannot reshape array of size 8438760 into shape (256,210969,40)

我该怎么办?谢谢

我认为您应该在模型创建之外重塑数据,因为这样您可以尝试从大小为 8438760 的向量创建 256 * 210969 * 40 的向量。我认为你实现你想要的方式是这样的:

X = X_training.reshape((layer_setup['config']['input_shape'])) # now X.shape = 210969 * 40
y = Y_training.reshape((Y_training.shape[0],Y_training.shape[1]))
...
model.fit(
            x = X,
            y = y,
            batch_size=256,
            epochs=500,
            validation=validationdata,
            verbose=0,
            shuffle=true,
            callbacks=callbacks,
            class_weight=class_weight
        )