简体   繁体   English

Keras LSTM 模型中的输入形状和拟合

[英]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.我正在学习 LSTM 模型以使数据集适合多类分类,即八种音乐流派,但不确定 Keras 模型中的输入形状。

I've followed the tutorials here:我已经按照这里的教程进行操作:

  1. How to reshape input data for LSTM model 如何重塑 LSTM 模型的输入数据
  2. Multi-Class Classification Tutorial with the Keras Deep Learning Library 使用 Keras 深度学习库的多类分类教程
  3. Sequence Classification with LSTM Recurrent Neural Networks in Python with Keras 使用 Keras 在 Python 中使用 LSTM 循环神经网络进行序列分类

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.我将数据形状转换为 (7678,1,30),即 7678 首音乐、1 个时间步和 30 个向量。 For the music genre, I used train_labels = pd.get_dummies(df['genre'])对于音乐流派,我使用了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 .但是在尝试拟合模型时,我收到错误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?我试过如何在 keras 中堆叠多个 lstm? 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] 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.顾名思义, return_sequences=True将返回一个序列(带有时间步长),这就是为什么你的输出形状是(None, 1, 8) :时间步长被保持。 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?我想如果您取消注释第二个 LSTM 层,这不会发生?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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