为什么LSTM Output_shape与input_shape

时间:2019-06-01 03:00:16

标签: tensorflow keras lstm

我有一个多层LSTM模型;我的问题是第一层与输入形状的输出形状不同(特征数量不同)。因此,我无法拟合模型。抛出错误。您能解释一下为什么会发生这种情况吗,任何解决方案都将不胜感激。

trainingModel = keras.Sequential()

print('training_batch_size : ',training_batch_size, 'DataX.shape[1] : ',trainingDataX.shape[1],'DataX.shape[2] : ', trainingDataX.shape[2])

trainingModel.add(keras.layers.LSTM(numberOfNeurons
                    , batch_input_shape=(training_batch_size, trainingDataX.shape[1], trainingDataX.shape[2])
                    , return_sequences=True
                    , stateful=True
                    , dropout = keyDropOut))

for idx in range(numberOfLSTMLayers - 1):

    trainingModel.add(keras.layers.LSTM(
        numberOfNeurons
        , return_sequences= True
        , dropout = keyDropOut * (idx +1)
        ))

trainingModel.compile(optimizer='adam',loss='mean_squared_error')#,metrics=['accuracy'])

#Model Layer Shapes ========================
for layer in trainingModel.layers:

    print('Input shape', layer.input_shape)
    print('Output shape', layer.output_shape)



Output
===============
training_batch_size :  96 trainingDataX.shape[1] :  10 trainingDataX.shape[2] :  4
Model Layer Shapes
Input shape (96, 10, 4)
Output shape (96, 10, 5) *<<<THIS IS MY PROBLEM
Input shape (96, 10, 5)
Output shape (96, 10, 5)
Input shape (96, 10, 5)
Output shape (96, 10, 5)


Finally when I fit the model, it trhows error like:

ValueError: A target array with shape (2880, 10, 4) was passed for an output of shape (96, 10, 5) while using as loss `mean_squared_error`. This loss expects targets to have the same shape as the output

1 个答案:

答案 0 :(得分:0)

在解决问题时回答我自己的问题:LSTM的第一层应具有神经元数量=特征数量;即在第一层中,我使用5个神经元。