This is Chinese version
[中文版]
1 Where this problem took place?
While using TensorFlow framework.
2 Description
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)
3 Why?
Because the shape of the input array is incompatible with the layer, which expected a 3D array, but we only have 2D.
4 Solutions
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 # import numpy module
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]