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

4

40

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.

Dolerite answered 16/6, 2017 at 7:23 Comment(1)
here the fastest and correct way to create data for LSTM/RNN: https://mcmap.net/q/408415/-keras-how-should-i-prepare-input-data-for-rnnMireillemireles
D
32

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)
Dolerite answered 16/6, 2017 at 7:59 Comment(0)
M
14

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

input_shape = (95000,360)

Same for the output.

Mhd answered 16/6, 2017 at 7:51 Comment(8)
No, that was also giving some errors like, expected was: None,95000,360 but found 1,95000,360Dolerite
That's a problem with your input data shape. The first dimension is supposed to be each sample. Your input should be (n_samples, timesteps, n_features). Looking at your other comment, 95000 is your n_samples not your timesteps. If you only have one feature with 360 timesteps, then the one you proposed is the right format.Mhd
Ok so, my input is having 360-360 blocks(which I want to be treated as features), so what should be the shape of input and what should be written inside here->{model.add(LSTM(22, input_shape=(?)))}Dolerite
I need to know what your input is. Is it a time series? How many different variables are you observing? I'll give you an example: if I'm observing the amount of rain and the temperature each hour for 24h in order to predict the weather (1 = good, 0 = bad), and I do that for 365 days, I will have 365 samples, each of which will have 24 timesteps, and 2 variables (one for rain, one for temperature), so my input is going to have the shape (365, 24, 2), and input_shape = (24, 2).Mhd
My input is a big array of arrays, each array in the big array is having a total of 360 signal values, this 360 chunk is classified as "L" arrhythmia or "R" arrhythmia etc or normal. and there are 95000 such arrays, the big array contains all these 95000 samples (initial shape: 95000,360) and output after one hot encoding has the shape:(95000,22) as there are 21 types of arrhythmia and 1 for normalDolerite
So you only have one variable, right? I.e. your signal is a single series of 360 samples of the same thing. In that case what you are doing is correct.Mhd
Yeah, I appreciate your help!Dolerite
@MicheleTonutti. It's still not working when I try to fit the model although my train shape is (804, 291) and also target (804, 291) as I am building autoencoder.Decode
C
14

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.

Creatural answered 10/3, 2019 at 10:23 Comment(1)
adding to this answer: usually if the error is in the first layer - then the problem might be with input_shape. if the problem is in the other layers, then the problem would maybe be in that you need to set return_sequences -TrueBorchardt
C
8

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

Consecutive answered 27/10, 2021 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.