【问题标题】:Dimension error building LSTM with keras使用 keras 构建 LSTM 的维度错误
【发布时间】:2017-10-18 13:14:51
【问题描述】:

我正在尝试使用 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)

有人知道这是什么问题吗?

欢迎和感谢任何 cmets 或建议!

下面是model.summary()的结果

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

【问题讨论】:

    标签: keras lstm


    【解决方案1】:

    将 return_sequences 添加到您的 LSTM 代码中:

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

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2023-03-05
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多