首页 > 解决方案 > 连接预测值 LSTM Keras

问题描述

我不知道我是否错了或者是否有意义,但我的想法是使用单一预测并基于该预测预测未来值(我在 Keras 上使用 LSTM 模型)。我想做的是:

1)获取前X个已知值(initial_values)

2) 使用 initial_values 进行预测并得到 (initial_prediction)。

3)删除initial_values的第一个元素并追加initial_prediction

4) 从步骤 2) 开始重复 X 次。

我的代码如下所示:

predictions = []
y_test_concat = []
num_steps_before = 30

# Step 1

# X_test_scaled_shape: (192, 1)
y_test_concat.append(X_test_scaled[:num_steps_before,:]) 
y_test_concat = np.array(y_test_concat)
y_test_concat.reshape(y_test_concat.shape[0],y_test_concat.shape[1],1)

# Step 2

simulated_predictions.append(model.predict(y_test_concat))

# Step 3 (where I get stucked)

num_steps_to_predict = 10
for i in range(1,num_steps_to_predict):
  ...

所以在下一次迭代中,数组应该是这样的:

[initial_value2,initial_value3,...initial_value30, initial_prediction]
[initial_value3,initial_value4,...initial_prediction, initial_prediction2]
...
[initial_value20,initial_value21,...initial_predictionX, initial_predictionY]

有任何想法吗?我想知道 Keras 中是否已经实现了使用 LSTM 来做这样的事情。

标签: pythontensorflowkeraslstm

解决方案


感谢@lukedeluccia 的回答,我想出了解决方案:

num_steps_before = 30
initial_values = np.array([X_test_scaled[:num_steps_before,:]])
predictions = initial_values

for i in range(0,num_steps_before):
  prediction = model.predict(predictions)
  predictions = np.append(predictions,[prediction])
  predictions = np.delete(predictions,0)
  predictions = predictions.reshape((1,predictions.shape[0],1))

推荐阅读