0

I am training a classification network with training data which has X.shape = (1119, 7) and Y.shape = (1119, 6). Below is my simple Keras network with and output dim of 6 (size of labels). The error which is returned is below the code

hidden_size = 128
model = Sequential()
model.add(Embedding(7, hidden_size))
#model.add(LSTM(128, input_shape=(1,7)))
model.add(LSTM(hidden_size, return_sequences=True))
model.add(LSTM(hidden_size, return_sequences=True))
model.add(Dense(output_dim=6, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', 
              optimizer='adam', 
              metrics=["categorical_accuracy"])

ValueError: Error when checking target: expected dense_13 to have shape (None, 6) but got array with shape (6, 1)

I would perfer not to do this in tensorflow because I am just prototyping yet it is my first run at Keras and am confused about why it cannot take this data. I attempted to reshape the data in a number of ways in which nothing worked. Any advice as to why this isn't work would be greatly appreciated.

2
  • I think Dense(output_dim=6, activation='softmax')) must be changed to Dense( 6 , activation='softmax')) Commented Mar 13, 2019 at 1:17
  • There's no problem with your model. You should give data processing and training code. Commented Mar 13, 2019 at 2:04

1 Answer 1

1

You should probably remove the parameter return_sequences=True from your last LSTM layer. When using return_sequences=True, the output of the LSTM layer has shape (seq_len, hidden_size). Passing this on to a Dense layer gives you an output shape of (seq_len, 6), which is incompatible with your labels. If you instead omit return_sequences=True, then your LSTM layer returns shape (hidden_size,) (it only returns the last element of the sequence) and subsequently your final Dense layer will have output shape (6,) like your labels.

Sign up to request clarification or add additional context in comments.

2 Comments

ValueError: Input 0 is incompatible with layer lstm_17: expected ndim=3, found ndim=2 is the error I receive if I attempt this.
Is lstm_17 the first or second LSTM layer? The first LSTM needs return_sequences=True, the second one doesn't. Also, your input X_train should have shape (batch_size, seq_length).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.