首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么LSTM Output_shape不同于input_shape

为什么LSTM Output_shape不同于input_shape
EN

Stack Overflow用户
提问于 2019-06-01 11:00:16
回答 1查看 135关注 0票数 1

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-06-01 21:06:20

回答我自己的问题时,我解决了这个问题: LSTM的第一层应该有神经元的数量=特征的数量;也就是说,在第一层中应该只有4个神经元,而我使用了5个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56403540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档