EDIT: Now I didn't convert to list.

I am training LSTM for multiple time-series in an array which has a structure: 450x801. There are 450 time series with each of 801 timesteps / time series. The labels are classes with assigned integer from 1 to 6, so the dimension of the label is 450x1. This is my implmentation:

This is my code:

def readData():
labels = pd.read_csv('label.csv', header = None)
labels = labels.values
data = pd.read_csv('data.csv', header = None)
return data, labels

data, labels = readData()

data_train, data_test, labels_train, labels_test = train_test_split(data, labels)

model = Sequential()
model.add(LSTM(units=32, input_shape = (450,801,1)))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Now I got this error:

Error when checking input: expected lstm_26_input to have 3 dimensions, but got array with shape (450, 801)

Any idea how to solve it?

For instance I can try to reshape:

data_train = np.reshape(data_train, (data_train.shape[0], 1, data_train.shape[1]))
data_test = np.reshape(data_test, (data_test.shape[0], 1, data_test.shape[1]))

And now the error is:

raise ValueError('Must pass 2-d input') ValueError: Must pass 2-d input

  • $\begingroup$ Please correct me if I'm wrong. You are trying a classification task with a multivariate LSTM model that takes 450 variables as input? $\endgroup$ – Leevo Sep 16 '19 at 8:38
  • $\begingroup$ @Leevo, yes, that's correct. Wouldn't it work? $\endgroup$ – user2754279 Sep 17 '19 at 10:00
  • $\begingroup$ It can work, but the computational effort is going to be huge $\endgroup$ – Leevo Sep 17 '19 at 10:12

I spot an error in your code. The input sequence for LSTM() layers must follow this schema:

( Number of observations , Number of Timesteps , Number of input series )

When you specify the input_shape argument you drop the number of observations. Therefore it becomes:

model = Sequential([

    LSTM(32, input_shape = (801, 450)), 

    Dense(6, activation='softmax')    

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

That's why you had an error about the number of input dimensions.

One final observations: 450 input features on 801 timesteps is a lot. Consider using some dimensionality reduction technique, because that's going to be very hard computationally speaking.

  • $\begingroup$ Thanks. I was thinking to either use TimeDistributed or maybe even employ Convolutional LSTM. Do you think ConvLSTM would be easier to compute? These are basically time-series from time-domain signal that can be converted into time-frequency-domain (2D-array) by using STFT. $\endgroup$ – user2754279 Sep 17 '19 at 23:04
  • $\begingroup$ Unfortunately I don't know your problem well enough to say if it's going to work. Conv2D layers are meant to receive 2D input matrices (such as image pixels), is that your case? $\endgroup$ – Leevo Sep 18 '19 at 7:30
  • $\begingroup$ Hi, I could convert my time-series into time-frequency domain, thus creating an image which is called "spectrogram". Is that enough explanation? It's normal in signal processing to convert 1D vector array time-series into 2D matrix of time-frequency domain. $\endgroup$ – user2754279 Sep 18 '19 at 22:54
  • $\begingroup$ As far as I know, yes it could work. Please consider that a Conv + Recurrent model is going to be computationally expensive. If you can train it it's worth give it a try though. $\endgroup$ – Leevo Sep 19 '19 at 7:38

It is becouse you convert your data to list data = data.values.tolist() But model input expects an array but not a list of arrays. So you need to convert your input list to the array.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.