简体   繁体   中英

The input shape and fitting in Keras LSTM model

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] ValueError: Input 0 of layer sequential_21 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 30]

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM