Hi I am trying to use a stacked LSTM architecture in keras, similar to what is shown here https://keras.io/getting-started/sequential-model-guide/. My problem is formulated as a binary time series classification, my timesteps are 2 and I have 7 attributes. The shape of my data is as follows:- X_train_smote_reshaped (1256L, 2L, 7L) y_train_smote_reshaped (1256L, 2L) X_validation_std_reshaped (168L, 2L, 7L) y_validation_reshaped (168L, 2L)

The error I get is: Exception: Error when checking model target: expected dense_1418 to have shape (None, 1) but got array with shape (1256L, 2L)

My keras code is listed below:

from keras.layers import LSTM
X_train_smote_reshaped=np.array([X_train_smote_std[i:i+2] for i in     range(len(X_train_smote_std)-2)])
y_train_smote_reshaped=np.array([y_train_smote[i:i+2] for i in range(len(y_train_smote)-2)])
X_validation_std_reshaped=np.array([X_validation_std[i:i+2] for i in range(len(X_validation_std)-2)])
y_validation_reshaped=np.array([y_validation[i:i+2] for i in range(len(y_validation)-2)])
#1.define the network
#one neuron in the output layer with a sigmoid activation function
#2. compile the network
#3. fit the model
  • 1
    $\begingroup$ Why does y have two columns? Is it the binary category (such as "yes or no" and "$x$ or $1-x$")? Or are the output timesteps too? $\endgroup$ – rilut Aug 25 '16 at 11:15

If y is the binary category (such as "yes or no" and "$x$ or $1−x$"), you should do

model.add(Dense(2, activation='sigmoid')) # because your output/y has two columns

rather than

model.add(Dense(1,activation='sigmoid')) # 1 neuron only

Or you can just use only 1 neuron and keep only 1 column from your y.

  • $\begingroup$ Thanks your suggestion works, I was trying to follow along with the example shown on Stacked LSTM for sequence classification-keras.io/getting-started/sequential-model-guide. The only thing is in there case they have 10 classes whereas I have 2, so was not sure whether the output layer would have 1 or 2 neurons. Thanks for your suggestion. $\endgroup$ – xjackx Aug 25 '16 at 19:30

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