![](/img/trans.png)
[英]ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
[英]ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (57, 1)
我正在使用 LSTM 模型並收到此錯誤。
ValueError: 層序的輸入 0 與層不兼容:預期 ndim=3,發現 ndim=2。 收到的完整形狀:(57, 1)
這是我的代碼:
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(64, input_shape = (700, 57), return_sequences=True))
model.add(tf.keras.layers.LSTM(64))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
optimizer = tf.keras.optimizers.Adam(lr=0.001)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
history = model.fit(train_data, batch_size=32, epochs=60, verbose=2, validation_data=valid_data)
model.save("LSTM.h5")
我的訓練數據的形狀是:
input_shape = (x_train.shape, y_train.shape)
print(input_shape)
((700, 57), (700,))
訓練數據集包含 700 行(樣本)和 57 列(特征),測試數據集包含 700 個樣本的標簽。
LSTM 需要三個維度的輸入,即(batch, timestep, features)
。 您的每個樣本都是長度為 1 的序列嗎? 在這種情況下,您需要設置input_shape = (1,57)
並將數據重塑為x_train = x_train[:, None, :]
和x_validation = x_validation[:, None, :]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.