ValueError: Input 0 of layer sequential is incompatible with



我正在构建一个RNN,我使用LSTM。X矩阵的维数为(1824,7)Y有这个dim(1824,1)。这是我的模型:

num_units = 64
learning_rate = 0.0001
activation_function = 'sigmoid'
adam = Adam(lr=learning_rate)
loss_function = 'mse'
batch_size = 5
num_epochs = 50
# Initialize the RNN
model = Sequential()
model.add(LSTM(units = num_units, activation=activation_function, input_shape=(1824, 7, )))
model.add(LeakyReLU(alpha=0.5))
model.add(Dropout(0.1))
model.add(Dense(units = 1))
# Compiling the RNN
model.compile(optimizer=adam, loss=loss_function, metrics=['accuracy'])
history = model.fit(
X,
y,
validation_split=0.1,
batch_size=batch_size,
epochs=num_epochs,
shuffle=False
)

我知道错误在input_shape参数。当我尝试拟合模型时,我得到这个错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 7]

我看到过类似的问题,我试着应用其中的一些改变,比如:

input_dim = X.shape
input_dim=(7,)
input_dim=(1824, 7, 1)

但无论如何,我得到了这种错误。我该怎么修理它?

@Nicolas Gervais评论

Tensorflow Keras LSTM期望输入:形状为[batch, timesteps, feature]的3D张量。

工作示例代码

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
print(inputs.shape)
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

输出
(32, 10, 8)
(32, 4)

相关内容

最新更新