首页 > 解决方案 > 如何在 keras 上使用 TimeDistributed Layer?

问题描述

因此,当我尝试使用 LSTM 训练模型时,我将输入数据重新整形为 (1000, 96, 1),并将输出数据重新整形为 (1000, 24, 1),这意味着我想用之前的 24 位数据预测未来96 个数据。当我添加一个时间分布的密集层作为最后一层时,我得到一个错误:

ValueError: Error when checking target: expected time_distributed_1 to have shape (96, 1) but got array with shape (24, 1)

那么有什么问题呢?这是我的代码:

modelA.add(LSTM(units=64, return_sequences=True,
                input_shape=[xProTrain_3D.shape[1], xProTrain_3D.shape[2]]))
modelA.add(LSTM(units=128, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=256, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=128, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=64, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(TimeDistributed(Dense(units=1, activation='relu', input_shape=(24, 1))))
modelA.compile(optimizer='Adam',
              loss='mse',
              metrics=['mse']) 
modelA.summary()
modelA.fit(x=xProTrain_3D, y=yProTrain_3D, epochs=epoch, batch_size=batch_size)

顺便说一下,输入形状是 (1000, 96, 1),输出形状是 (1000, 24, 1)

标签: keraslstmrecurrent-neural-network

解决方案


推荐阅读