0

I have a problem with training model in LSTM. the error is : ValueError: Input 0 of layer sequential_8 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 5, 1, 1)

Im thankful to any body to solve my problem

the code is :

def df_to_X_y(df,window_size=5):
    df_as_np = df.to_numpy()
    X = []
    y = []
    for i in range(len(df_as_np)-window_size):
        row = [[a] for a in df_as_np[i:i+5]]
        X.append(row)
        label = df_as_np[i+5]
        y.append(label)
    return np.array(X), np.array(y)

X, y = df_to_X_y(scaled_data_frame,window_size=5)
X.shape,y.shape

the answer is :((306234, 5, 1, 1), (306234, 1))

X_train,y_train = X[:245000],y[:245000]
X_val,y_val = X[245000:275620],y[245000:275620]
X_test,y_test = X[275620:],y[275620:]
X_train.shape,y_train.shape,X_val.shape,y_val.shape,X_test.shape,y_test.shape

the answer is : ((245000, 5, 1, 1), (245000, 1), (30620, 5, 1, 1), (30620, 1), (30614, 5, 1, 1), (30614, 1))

from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.metrics import RootMeanSquaredError
from tensorflow.keras.optimizers import Adam

model = Sequential()
model.add(InputLayer((5,1)))
model.add(LSTM(128))
model.add(Dense(8,'relu'))
model.add(Dense(1,'linear'))

cp = ModelCheckpoint('model',save_best_only=True)
model.compile(loss=MeanSquaredError(), optimizer=Adam(learning_rate=0.0001),
             metrics=[RootMeanSquaredError()])

model.fit(X_train,y_train, validation_data=(X_val,y_val), epochs=10,
          callbacks=[cp])
A.Najafi
  • 691
  • 1
  • 9
  • 20
  • expected input data shape must be (batch_size, timesteps, data_dim), but your `X_train` NumPy-array has 4 dimensions. – A.Najafi Nov 24 '21 at 10:30

1 Answers1

0

The expected input data shape is (batch_size, timesteps, data_dim), but your X_train NumPy-array has 4 dimensions. So, in order to solve the problem you should reshape your input data this way:

X_train = X_train.reshape((-1,5,1))
X_val   = X_val.reshape((-1,5,1))
A.Najafi
  • 691
  • 1
  • 9
  • 20
  • hello my friend , after using your suggestion the model ran just one epoch and detect the error like this: ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 5, 1, 1) I used your code before fitting data to model – Amirhossein Nov 24 '21 at 17:52
  • 1
    I also use your code for X_val and model ran but the loss and root_mean_squared_error are nan! – Amirhossein Nov 24 '21 at 18:03
  • @Amirhossein if it runs, then accept my answer. About your nan problem, it can be the result of gradient explosion. check this link it may help you. https://stackoverflow.com/questions/40050397/deep-learning-nan-loss-reasons – A.Najafi Nov 24 '21 at 19:00