ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4

90,147

Solution 1

I solved the problem by making

input size: (95000,360,1) and output size: (95000,22)

and changed the input shape to (360,1) in the code where model is defined:

model = Sequential()
model.add(LSTM(22, input_shape=(360,1)))
model.add(Dense(22, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(ml2_train_input, ml2_train_output_enc, epochs=2, batch_size=500)

Solution 2

input_shape is supposed to be (timesteps, n_features). Remove the first dimension.

input_shape = (95000,360)

Same for the output.

Solution 3

Well, I think the main problem out there is with the return_sequences parameter in the network.This hyper parameter should be set to False for the last layer and true for the other previous layers.

Solution 4

In Artifical Neural Networks (ANN), input is of shape (N,D), where N is the number of samples and D is the number of features.

IN RNN, GRU and LSTM, input is of shape (N,T,D), where N is the number of samples, T is length of time sequence and D is the number of features.

So, while adding layers

Input(shape = (D,)) for ANN and Input(shape = (T,D)) for RNN, GRU and LSTMs

Share:
90,147
Urja Pawar
Author by

Urja Pawar

Well, I fortunately know the fact of automated coding machines, But to do something for humanity is not machines' piece of cake. Its Mine!

Updated on July 09, 2022

Comments

  • Urja Pawar
    Urja Pawar almost 2 years

    I am trying for multi-class classification and here are the details of my training input and output:

    train_input.shape= (1, 95000, 360) (95000 length input array with each element being an array of 360 length)

    train_output.shape = (1, 95000, 22) (22 Classes are there)

    model = Sequential()
    
    model.add(LSTM(22, input_shape=(1, 95000,360)))
    model.add(Dense(22, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    model.fit(train_input, train_output, epochs=2, batch_size=500)
    

    The error is:

    ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4 in line: model.add(LSTM(22, input_shape=(1, 95000,360)))

    Please help me out, I am not able to solve it through other answers.