1

I would appreciate your help with creating an LSTM model in Keras. My input is a 2D numpy array with rows that are numeric time-series of equal length. I've written the following lines to create an LSTM model that operates on the raw data without an embedding layer:

lstm_clf = Sequential()
lstm_clf.add(LSTM(100, input_shape=(X_train[0], X_train[1]))
lstm_clf.add(Dense(7, activation='softmax'))
lstm_clf.compile(loss='sparse_categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])

When I reach the fitting phase, I get the following error: "ValueError: Error when checking input: expected lstm_11_input to have 3 dimensions, but got array with shape (100, 289)".

Could anyone please explain to me what I'm doing wrong and how to fix the code. It must be related to the input shape, but I've no idea what it is.

Thanks a lot for your help,

Alexander.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76

1 Answers1

0

I was able to find the answer. Here's the correct code:

lstm_clf = Sequential()

lstm_clf.add(LSTM(100, input_shape=(1, max_seq_len))) lstm_clf.add(Dense(7, activation='softmax')) lstm_clf.compile(loss='sparse_categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])

My data also had to be reshaped, as following:

X_seqs_train = reshape(X_seqs_train, (X_seqs_train.shape[0], 1, X_seqs_train.shape[1]))

X_seqs_test = reshape(X_seqs_test, (X_seqs_test.shape[0], 1, X_seqs_test.shape1))

These changes solved the problem. A clue that led to the answer was found here.