Jam*_*mon 5 python keras tensorflow tensorflow-datasets tensorflow2.0
我正在考虑为时间序列 LSTM 模型创建管道。我有两个输入提要,我们称它们为series1和series2。
我tf.data通过调用来初始化对象from.tensor.slices:
ds = tf.data.Dataset.from_tensor_slices((series1, series2))
Run Code Online (Sandbox Code Playgroud)
我将它们进一步批处理到设定窗口大小的窗口中,并在窗口之间移动 1:
ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
Run Code Online (Sandbox Code Playgroud)
此时我想尝试一下它们是如何批处理在一起的。我想生成如下所示的特定输入作为示例:
series1 = [1, 2, 3, 4, 5]
series2 = [100, 200, 300, 400, 500]
batch 1: [1, 2, 100, 200]
batch 2: [2, 3, 200, 300]
batch 3: [3, 4, 300, 400]
Run Code Online (Sandbox Code Playgroud)
因此,每个批次将返回Series1的两个元素,然后返回Series2的两个元素。此代码片段无法单独批处理它们:
ds = ds.map(lambda s1, s2: (s1.batch(window_size + 1), s2.batch(window_size + 1))
Run Code Online (Sandbox Code Playgroud)
因为它返回数据集对象的两个映射。由于它们是对象,因此它们不可订阅,因此这也不起作用:
ds = ds.map(lambda s1, s2: (s1[:2], s2[:2]))
Run Code Online (Sandbox Code Playgroud)
我确信解决方案是使用.apply自定义 lambda 函数。任何帮助深表感谢。
我还在考虑制作一个代表该系列下一个元素的标签。例如,批次将产生以下内容:
batch 1: (tf.tensor([1, 2, 100, 200]), tf.tensor([3]))
batch 2: (tf.tensor([2, 3, 200, 300]), tf.tensor([4]))
batch 3: (tf.tensor([3, 4, 300, 400]), tf.tensor([5]))
Run Code Online (Sandbox Code Playgroud)
其中[3]、[4]和表示 的下一个要预测的[5]元素。series1
解决方案是分别对两个数据集进行窗口化,.zip()然后将它们放在一起,然后.concat()是包含标签的元素。
ds = tf.data.Dataset.from_tensor_slices(series1)
ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
ds = ds.flat_map(lambda window: window.batch(window_size + 1))
ds = ds.map(lambda window: (window[:-1], window[-1]))
ds2 = tf.data.Dataset.from_tensor_slices(series2)
ds2 = ds2.window(window_size, shift=1, drop_remainder=True)
ds2 = ds2.flat_map(lambda window: window.batch(window_size))
ds = tf.data.Dataset.zip((ds, ds2))
ds = ds.map(lambda i, j: (tf.concat([i[0], j], axis=0), i[-1]))
Run Code Online (Sandbox Code Playgroud)
(<tf.Tensor: shape=(7,), dtype=int32, numpy=array([ 1, 2, 3, 100, 200, 300])>, <tf.Tensor: shape=(), dtype=int32, numpy=4>)
(<tf.Tensor: shape=(7,), dtype=int32, numpy=array([ 2, 3, 4, 200, 300, 400])>, <tf.Tensor: shape=(), dtype=int32, numpy=5>)
(<tf.Tensor: shape=(7,), dtype=int32, numpy=array([ 3, 4, 5, 300, 400, 500])>, <tf.Tensor: shape=(), dtype=int32, numpy=6>)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1710 次 |
| 最近记录: |