python - 连接预测值 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 来做这样的事情。
解决方案
感谢@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))
推荐阅读
- go - Go 编译的二进制文件会在 Windows NT 上运行,包括使用以太网端口吗?
- python - Pygame 中的 2D Raycasting 和 rect 碰撞(视线、障碍物)
- firebase - nativescript:来自 Admob 的广告在生产中不起作用
- html - CSS 你如何将一个项目与另一个项目对齐?
- powershell - 通过 Powershell 将文本文件内容与文件夹内容进行比较
- php - 如何计算非重复字符,不包括 PHP 中重复的字符
- mysql - MySQL from_unixtime 现在和午夜
- android - 如何在 espresso 测试中获得 Activity?
- kubernetes - 我应该如何配置我的服务来访问 grafana:localhost:3000 with minikube
- python - sys.stdin 什么时候等待输入?