使用keras构建LSTM的维度错误

时间:2017-10-18 13:14:51

标签: keras lstm

我正在尝试使用Keras构建LSTM网络。 我的时间系列示例大小为492。我想用3前面的例子来预测下一个例子。因此,输入转换为大小(num_samples,3*492),输出大小为(num_samples,492)

根据this blog,我首先将数据大小转换为表单(num_samples,timesteps,features)

#convert trainning data to 3D LSTM shape
train_origin_x = train_origin_x.reshape((train_origin_x.shape[0],3,492))
test_origin_x  = test_origin_x.reshape((test_origin_x.shape[0],3,492))
print(train_origin_x.shape,test_origin_x.shape)
(216, 3, 492) (93, 3, 492)
print(train_origin_y,test_origin_y)
(216, 492) (93, 492)

以下是构建LSTM网络的代码

#building network
model = Sequential()
model.add(LSTM(hidden_units,return_sequences=True,input_shape=(train_origin_x.shape[1],train_origin_x.shape[2])))
model.add(Dense(492))
model.compile(loss='mse',optimizer='adam')
print('model trainning begins...')
history = model.fit(train_origin_x,train_origin_y,epochs = num_epochs,batch_size = num_batchs,
          validation_data=(test_origin_x,test_origin_y))

但是我在这个过程中遇到了错误,说

ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (216, 492)

任何人都知道问题是什么?

欢迎并赞赏任何意见或建议!!

以下是model.summary()

的结果
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 3, 50)             108600    
_________________________________________________________________
dense_1 (Dense)              (None, 3, 492)            25092     
=================================================================

1 个答案:

答案 0 :(得分:0)

将return_sequences添加到LSTM代码中:

model.add(LSTM(hidden_units, return_sequences = False,input_shape=(train_origin_x.shape[1],train_origin_x.shape[2])))