# Stacked LSTM for sequence classification - Keras - Error [closed]

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)])
data_dim=7
timesteps=2
#1.define the network
model=Sequential()
model.add(LSTM(20,return_sequences=True,input_shape=(timesteps,data_dim)))
model.add(LSTM(20))
#one neuron in the output layer with a sigmoid activation function
model.add(Dense(1,activation='sigmoid'))
#2. compile the network
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
#3. fit the model
model.fit(X_train_smote_reshaped,y_train_smote_reshaped,batch_size=64,nb_epoch=5,validation_data=(X_validation_std_reshaped,y_validation_reshaped))


## closed as off-topic by Silverfish, whuber♦Aug 25 '16 at 13:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Silverfish, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

• 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? – rilut Aug 25 '16 at 11:15

## 1 Answer

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.

• 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. – xjackx Aug 25 '16 at 19:30