如何在 TensorFlow 2.0 中使用 Dataset.window() 方法创建的窗口? [英] How to use windows created by the Dataset.window() method in TensorFlow 2.0?
本文介绍了如何在 TensorFlow 2.0 中使用 Dataset.window() 方法创建的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 TensorFlow 2.0 创建一个数据集,该数据集将从时间序列中返回随机窗口,以及作为目标的下一个值.
I'm trying to create a dataset that will return random windows from a time series, along with the next value as the target, using TensorFlow 2.0.
我正在使用 Dataset.window()
,看起来很有希望:
I'm using Dataset.window()
, which looks promising:
import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices(tf.range(10))
dataset = dataset.window(5, shift=1, drop_remainder=True)
for window in dataset:
print([elem.numpy() for elem in window])
输出:
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]
[5, 6, 7, 8, 9]
但是,我想使用最后一个值作为目标.如果每个窗口都是一个张量,我会使用:
However, I would like to use the last value as the target. If each window was a tensor, I would use:
dataset = dataset.map(lambda window: (window[:-1], window[-1:]))
但是,如果我尝试这样做,则会出现异常:
However, if I try this, I get an exception:
TypeError: '_VariantDataset' object is not subscriptable
推荐答案
解决方案是这样调用flat_map()
:
dataset = dataset.flat_map(lambda window: window.batch(5))
现在数据集中的每一项都是一个窗口,所以你可以像这样拆分它:
Now each item in the dataset is a window, so you can split it like this:
dataset = dataset.map(lambda window: (window[:-1], window[-1:]))
所以完整的代码是:
import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices(tf.range(10))
dataset = dataset.window(5, shift=1, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(5))
dataset = dataset.map(lambda window: (window[:-1], window[-1:]))
for X, y in dataset:
print("Input:", X.numpy(), "Target:", y.numpy())
输出:
Input: [0 1 2 3] Target: [4]
Input: [1 2 3 4] Target: [5]
Input: [2 3 4 5] Target: [6]
Input: [3 4 5 6] Target: [7]
Input: [4 5 6 7] Target: [8]
Input: [5 6 7 8] Target: [9]
这篇关于如何在 TensorFlow 2.0 中使用 Dataset.window() 方法创建的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文