python-3.x - 如何在 Tensorflow 中拆分 LSTM 的训练数据和测试数据以进行时间序列预测
问题描述
我最近从https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/23_Time-Series-Prediction.ipynb学习了用于时间序列预测的 LSTM
在他的教程中,他说:我们将使用以下函数从训练数据中随机挑选一批较短的子序列,而不是在近 300k 观察的完整序列上训练递归神经网络。
def batch_generator(batch_size, sequence_length):
"""
Generator function for creating random batches of training-data.
"""
# Infinite loop.
while True:
# Allocate a new array for the batch of input-signals.
x_shape = (batch_size, sequence_length, num_x_signals)
x_batch = np.zeros(shape=x_shape, dtype=np.float16)
# Allocate a new array for the batch of output-signals.
y_shape = (batch_size, sequence_length, num_y_signals)
y_batch = np.zeros(shape=y_shape, dtype=np.float16)
# Fill the batch with random sequences of data.
for i in range(batch_size):
# Get a random start-index.
# This points somewhere into the training-data.
idx = np.random.randint(num_train - sequence_length)
# Copy the sequences of data starting at this index.
x_batch[i] = x_train_scaled[idx:idx+sequence_length]
y_batch[i] = y_train_scaled[idx:idx+sequence_length]
yield (x_batch, y_batch)
他尝试创建几个训练样本。
我的问题是,我们可以先随机穿梭x_train_scaled
and y_train_scaled
,然后使用 follow 开始采样几个批量大小batch_generator
吗?
我对这个问题的动机是,对于时间序列预测,我们想要训练过去并预测未来。那么,穿梭训练样本是否合法?
在教程中,作者选择了一块连续的样本如
x_batch[i] = x_train_scaled[idx:idx+sequence_length]
y_batch[i] = y_train_scaled[idx:idx+sequence_length]
我们可以选择x_batch
而y_batch
不是连续的。例如,在同一天x_batch[0]
拣货10:00am
和x_batch[1]
拣货?9:00am
总结:以下两个问题是
(1)我们可以先随机穿梭x_train_scaled
and y_train_scaled
,然后使用follow开始抽样几个batch sizebatch_generator
吗?
(2)我们在训练LSTM的时候,需要考虑时间顺序的影响吗?我们为 LSTM 学习了哪些参数。
谢谢
解决方案
(1) 我们不能。想象一下试图预测明天的天气。您想要最近 10 小时的一系列温度值,还是想要最近 5 年的随机温度值?
您的数据集是以 1 小时为间隔的一长串值。你的 LSTM 接收一系列按时间顺序连接的样本。例如,sequence_length = 10
它可以将 2018-03-01 09:00:00 到 2018-03-01 19:00:00 的数据作为输入。如果在生成包含这些序列的批次之前对数据集进行混洗,您将训练 LSTM 根据整个数据集中的随机样本序列进行预测。
(2) 是的,我们需要考虑时间序列的时间顺序。您可以在这里找到在 python 中测试时间序列 LSTM 的方法:https ://machinelearningmastery.com/backtest-machine-learning-models-time-series-forecasting/
训练/测试数据必须以尊重时间顺序的方式进行拆分,并且模型永远不会在未来的数据上进行训练,而只会在未来的数据上进行测试。
推荐阅读
- javascript - 如何将 React 类组件转换为函数组件
- azure-machine-learning-service - ModuleNotFoundError:Azure ML 管道中没有名为“keras”的模块
- asp.net-core - 如何在 linux 上生成合理大小的 .NET Core 进程的内存转储文件?
- codeigniter - 饼图chart.js未满
- typescript - 从类型中获取泛型类型
- elasticsearch - Kibana:聚合“日期直方图”无法正常工作
- bash - 在 bash 中进行计数和拆分
- asp.net-core - 在 .net Core 中使用 JWT 进行跨域单点登录 (SSO)
- python - 如何恢复不属于主列表的类别名称?
- mongoose - findOneAndUpdate 用于具有两个条件的数组的嵌套对象元素