【发布时间】:2019-06-01 21:06:19
【问题描述】:
我有一个多层 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
【问题讨论】:
-
没关系;解决了,LSTM的第一层应该有神经元数=特征数;即在第一层应该只有 4 个神经元,而我使用了 5 个。
-
目标数组第1维为2880,输出数组第96维。输入数组的样本数与标签数不匹配。
-
@ShubhamPanchal,感谢您的调查。输出数组 96 的第一维仅表示批量大小,所以这不是问题。无论如何让它与第一个 LSTM 层中的神经元数量 = 数量特征一起工作。
标签: tensorflow keras lstm