2

My Y_train is a one-hot encoded label matrix.

The shape of my Y_train is (10, 1000, 3) because I have three different categories.

My model is defined as:

model = Sequential()
model.add(LSTM(100, input_shape=(1000, 38), return_sequences=False))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

When I train my model, I get the following error:

Error when checking target: expected dense_83 to have 2 dimensions, but got array with shape (8, 1000, 3)

This occurs because my Y_train is a 3d matrix, instead of a 2d matrix. The only way I've been able to solve this is by setting return_sequences=True but not sure if that will affect my LSTM's output.

Is this the correct way to deal with categorical labels? By setting return_sequences=True as a parameter of LSTM?

In other words, is it okay to return_sequences before a Softmax layer?

Thank you!

Eduardo Morales
  • 764
  • 7
  • 29
  • 1
    What is your question? – desertnaut Mar 10 '19 at 23:44
  • I added an edit to emphasize my question at the end, sorry for the confusion! – Eduardo Morales Mar 10 '19 at 23:48
  • 1
    Can you elaborate the meaning of second dimension(1000) of `Y_train`? – keineahnung2345 Mar 11 '19 at 08:02
  • @keineahnung2345 i have 10 sequences with 1000 timesteps and 38 features in each timestep! Thanks – Eduardo Morales Mar 11 '19 at 16:07
  • 1
    https://stackoverflow.com/questions/46102332/lstm-keras-api-predicting-multiple-outputs This should be helpful. – keineahnung2345 Mar 12 '19 at 00:13
  • @keineahnung2345 thanks for sharing that. I'm only able to train with one hot encoded labels when return_sequences is set to True. Any idea why? Otherwise it says it expects the output to have 2 dimensions. One-hot encoded make it 3 dimensions per timestep. (seq_n, timestep, onehot_label) – Eduardo Morales Mar 12 '19 at 08:03
  • 1
    One-hot encoding should be ok. It seems you have a label for every timestamp in every sequence, in this case, you should use `return_sequences=True` because otherwise your model output and your label don't match. – keineahnung2345 Mar 12 '19 at 08:07

0 Answers0