目前,我能够创建一个timeseries滑动窗口批处理数据集,其中包含有序的“特征集”,如“输入”、“目标”、“基准”等。最初,我开发了我的模型和数据集,其中目标的批次大小与所有其他输入的批次大小相同,但是这对调整输入批次大小是有害的,而且在实时数据上运行也没有帮助,因为我只关心生成形状(1, horizon, targets)的单个样本输出,或者可能只是(horizon, targets)给定(samples, horizon, features)的输入数据集。
作为概述,我想在time T上获取horizon length features的horizon历史样本,在模型中运行它们并输出horizon length targets的单个示例;重复直到数据集完整地运行为止。
假设熊猫的长度为DataFrame Z,所有得到的数据集都应该有一个Z - horizon长度。“目标”数据集的批处理大小应为1,“输入”数据集的批处理大小应为batch_size。
下面是我目前使用的代码片段,用于为所有功能集生成标准批处理大小:
import tensorflow as tf
import pandas as pd
horizon = 5
batch_size = 10
columns = {
"inputs": ["input_1", "input_2"],
"targets": ["target_1"],
}
batch_options = {
"drop_remainder": True,
"deterministic": True,
}
d = range(100)
df = pd.DataFrame(data={'input_1': d, 'input_2': d, 'target_1': d})
slices = tuple(df[x].astype("float32") for x in columns.values())
data = (
tf.data.Dataset.from_tensor_slices(slices)
.window(horizon, shift=1, drop_remainder=True)
.flat_map(
lambda *c: tf.data.Dataset.zip(
tuple(
col.batch(horizon, **batch_options)
for col in c
)
)
)
.batch(
batch_size,
**batch_options,
)
)发布于 2022-11-24 15:39:00
我们可以创建两个滑动窗口数据集并压缩它们。
inputs = df[['input_1', 'input_1']].to_numpy()
labels = df['target_1'].to_numpy()
window_size = 10
stride =1
data1 = tf.data.Dataset.from_tensor_slices(inputs).window(window_size, shift=stride, drop_remainder=True).flat_map(lambda x: x.batch(window_size))
data2 = tf.data.Dataset.from_tensor_slices(inputs).window(1, shift=stride, drop_remainder=True).flat_map(lambda x: x.batch(1))
data = tf.data.Dataset.zip((data1, data2))https://stackoverflow.com/questions/74552302
复制相似问题