deep-learning - 如何使用 RNN 使用 6 个时间步来预测接下来的 4 个时间步
问题描述
我得到了一个包含 6 个数据点 +4 个数据点作为标签的数据集,他们要求使用 6 个数据步来预测这 4 个时间步。
你能告诉我什么模型以及我应该如何使用它,我认为某种 RNN 是因为每个点都有时间。
谢谢!
解决方案
这类预测依赖于先前输入的问题通常使用 RNN 网络(rnn、gru 和 lstm),因为它们保留了先前的状态信息。为了更深入的了解: https ://colah.github.io/posts/2015-08-Understanding-LSTMs/
请仔细阅读我在代码中编写的评论。
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras import Model
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import RNN, LSTM
"""
creating a toy dataset
lets use this below ```input_sequence``` as the sequence to make data points.
as per the question, we will use 6 points to predict next 4 points
"""
input_sequence = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
X_train = []
y_train = []
#first 6 points will be our input data points and next 4 points will be data label.
# so on we will shift by 1 and make such data points and label pairs
for i in range(len(input_sequence)-9):
X_train.append(input_sequence[i:i+6])
y_train.append(input_sequence[i+6:i+10])
X_train = np.array(X_train, dtype=np.float32)
y_train = np.array(y_train, dtype=np.int32)))
#X_test for the predictions (contains 6 points)
X_test = np.array([[8,9,10,1,2,3]],dtype=np.float32)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
#we will be using basic LSTM, which accepts input in ```[num_inputs, time_steps, data_points], therefore reshaping as per that```
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
x_points = X_train.shape[-1]
print("one input contains {} points".format(x_points))
model = Sequential()
model.add(LSTM(4, input_shape=(1, x_points)))
model.add(Dense(4))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
model.fit(X_train, y_train, epochs=500, batch_size=5, verbose=2)
output = list(map(np.ceil, model.predict(X_test)))
print(output)
我们使用了更简单的模型,这可以进一步改进以获得更好的结果。
推荐阅读
- python - 如何提取提取的列表元素之后的列表元素?
- react-native - 如何将 react-native-maps 导入 Clojruescript Expo 项目
- python - 如何将我的 800x480 5 英寸屏幕分成两部分来制作 VR 耳机
- css - 为什么我的圆锥渐变的边缘不锋利?
- android - Flutter 日志垃圾邮件
- c# - 为什么这个 MVC 方法不能以并行方式运行?
- javascript - 如何通过使用纯 JavaScript 选中复选框来显示表格?
- javascript - 子组件未定义
- c++ - C/C++ 内存模型是否允许相同字节的不同粒度的原子?
- c++ - 函数的特化。指针类型名和字符串之间的区别?