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.

Urja Pawar
  • 1,087
  • 1
  • 15
  • 29

4 Answers4

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)
Urja Pawar
  • 1,087
  • 1
  • 15
  • 29
14

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

input_shape = (95000,360)

Same for the output.

Michele Tonutti
  • 4,298
  • 1
  • 21
  • 22
  • No, that was also giving some errors like, expected was: None,95000,360 but found 1,95000,360 – Urja Pawar Jun 16 '17 at 07:54
  • 1
    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. – Michele Tonutti Jun 16 '17 at 08:21
  • 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=(?)))} – Urja Pawar Jun 16 '17 at 09:01
  • 5
    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). – Michele Tonutti Jun 16 '17 at 09:06
  • 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 normal – Urja Pawar Jun 16 '17 at 09:11
  • 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. – Michele Tonutti Jun 16 '17 at 09:50
  • Yeah, I appreciate your help! – Urja Pawar Jun 16 '17 at 09:58
  • @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. – Avv Jul 08 '21 at 23:09
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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 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 -True` – Alon Gouldman May 19 '20 at 13:02
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

Shubham Kaushik
  • 123
  • 2
  • 6