【问题标题】:ValueError: Input 0 of layer lstm_27 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 5)ValueError: 层 lstm_27 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:(无,5)
【发布时间】:2022-08-19 19:55:22
【问题描述】:

我有一些像素运动数据,它有 5 个特征和 3715489 个训练样本。我不断收到这个错误,我不知道我应该为 LSTM 制作什么 input_shape。

X_train 形状为 (3715489,5)。我需要重塑这个吗?

y_train 形状为 (3715489, 8)

这是我的代码:

model = Sequential()
model.add(LSTM(256,return_sequences=True, input_shape=(5,)))
model.add(Dense(8, activation=\'sigmoid\'))
model.compile(optimizer=\'adam\',loss=\'categorical_crossentropy\', metrics=[\'accuracy\'])

print(model.summary())
model.fit(x_train, y_train, epochs=100,batch_size=320)
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: tensorflow machine-learning keras lstm recurrent-neural-network


【解决方案1】:

如错误中所述,

ValueError: 层 lstm_27 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2

LSTM 层需要形状为(batch_size, timesteps, input_dim) 的 3D 输入。您可以将 (timesteps, input_dim) 传递给 input_shape 参数。但是您正在设置 input_shape (5,)。此形状不包括时间步长维度。为 input_shape 添加一个额外的维度将解决这个问题。

#Reshape data
x_train = tf.reshape(x_train,(-1, 1, 5))
y_train = tf.reshape(y_train,(-1, 1, 8))

model = Sequential()
model.add(tf.keras.layers.LSTM(256,return_sequences=True, input_shape=(1,5)))
model.add(tf.keras.layers.Dense(8, activation='sigmoid'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])

print(model.summary())
model.fit(x_train, y_train, epochs=1,batch_size=320)

请参阅此link 了解更多信息。谢谢!

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2021-08-25
    • 2021-01-21
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多