Input 0 of layer “lstm_1“ is incompatible with the layer: expected ndim=3, found ndim=2. Full shape=

While using TensorFlow framework.

When I fit the LSTM model based on TensorFlow, I got an error report
Input 0 of layer "lstm_37" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (16, 7)

Model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, shuffle = True, verbose = 1, validation_split = VALIDATION_SPLIT)

Because the shape of the input array is incompatible with the layer, which expected a 3D array, but we only have 2D.

These are original words,

X_train = df_1.values

4.1 Solution 1

.reshape()

X_train = X_train.reshape(dimi_x,dim_y,dim_z)

.reshape could set the shape of the array directly and manually.

4.2 Solution 2

np.expand_dims()
numpy is for doing the math

import numpy as np

X_train = np.expand_dims(X_train, axis=0)

This is a simple way to solve this problem.

4.3 Solution 3

Lambda(lambda _:K.expand_dims(_,axis=-1))()

import keras.backend as K
from keras.layers import Lambda

X_train = Lambda(lambda d_1:K.expand_dims(X_train,axis=-1))(X_train)

Warning: This method may create new error like below,
ValueError: Input 0 of layer "lstm_43" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 63360, 7, 1)
Because the shape of input array has 4 dimensions which is more than expected dimension number, now you could just try to delete
input_shape = X_train.shape

4.4 Solution 4

X_train = X_train[:, None]

Original: https://blog.csdn.net/qq_46261795/article/details/124230451
Author: Benny_WEI
Title: Input 0 of layer “lstm_1“ is incompatible with the layer: expected ndim=3, found ndim=2. Full shape=

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/496594/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球