0
votes

I am learning the LSTM model to fit the data set to the multi-class classification, which is eight genres of music, but unsure about the input shape in the Keras model.

I've followed the tutorials here:

  1. How to reshape input data for LSTM model
  2. Multi-Class Classification Tutorial with the Keras Deep Learning Library
  3. Sequence Classification with LSTM Recurrent Neural Networks in Python with Keras

My data is like this:

vector_1,vector_2,...vector_30,genre
  23.5     20.5          3      pop
   .
   .
   .
(7678)

I transformed my data shape into (7678,1,30), which is 7678 pieces of music, 1 timestep, and 30 vectors. For the music genre, I used train_labels = pd.get_dummies(df['genre'])

Here is my model:

# build a sequential model
model = Sequential()

# keras convention to use the (1,30) from the scaled_train

model.add(LSTM(32,input_shape=(1,30),return_sequences=True))
model.add(LSTM(32,return_sequences=True))
model.add(LSTM(32))
# to avoid overfitting
model.add(Dropout(0.3))

# output layer
model.add(Dense(8,activation='softmax'))

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])

Fitting the model

model.fit(scaled_train,train_labels,epochs=5,validation_data=(scaled_validation,valid_labels))

But when trying to fit the model, I got the error ValueError: Shapes (None, 8) and (None, 1, 8) are incompatible. Is there anything I did wrong in the code? Any help is highly appreciated.

The shape of my data

print(scaled_train.shape)
print(train_labels.shape)
print(scaled_validation.shape)
print(valid_labels.shape)
(7678, 1, 30)
(7678, 8)
(450, 30)
(450, 8)

EDIT

I've tried How to stack multiple lstm in keras? But still, get the error ValueError: Input 0 of layer sequential_21 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 30]

1

1 Answers

1
votes

As the name suggests, return_sequences=True will return a sequence (with a time step), That's why your output shape is (None, 1, 8): the time step is maintained. It doesn't flatten automatically when it goes through the dense layer. Try:

model = Sequential()
model.add(LSTM(32,input_shape=(1,30),return_sequences=False))
model.add(Dense(32,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(8,activation='softmax'))

I guess this doesn't happen if you uncomment the second LSTM layer?