重建Keras的LSTM列车和测试装置

时间:2017-12-05 22:34:05

标签: python keras lstm forecasting

所以我在Keras Github上看到了一些SO帖子和问题帖子,但到目前为止,这些解决方案似乎都不适用于我。

我的问题与Keras的LSTM的input_shape有关。与大多数示例不同,我的问题是时间序列预测问题,与我在各处看到的分类示例类型无关。

我想知道在Keras中安装LSTM模型时如何重塑我的训练和测试数据集。 对于我的使用案例,我想为每个批次包含82个时间步长和40个功能的LSTM建模

# train_X, test_X, train_y & test_y are numpy arrays
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

> ((57482, 40), (57482,), (12546, 40), (12546,))

# For my use case I'd like to model an LSTM with 82 time-steps and 40 features per batch

# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0]/82, 82, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0]/82, 82, test_X.shape[1]))

train_y = train_y.reshape((train_y.shape[0]/82, 82, 1))
test_y = test_y.reshape((test_y.shape[0]/82, 82, 1))

print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

> ((701, 82, 40), (701, 82, 1), (153, 82, 40), (153, 82, 1))

当我现在创建和LSTM然后尝试适应它时,

model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')

# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=1, validation_data=(test_X, test_y), verbose=2, shuffle=False)

我收到以下错误,

ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (701, 82, 1)

我想我理解了input_shape和batch_size的概念,但我显然无法重塑我的火车和测试装置以匹配Keras的要求。我做错了什么?

感谢您的帮助!

0 个答案:

没有答案