我正在尝试使用 TensorFlow 2.0 创建一个数据集,该数据集将返回时间序列中的随机窗口,以及作为目标的下一个值。
我正在使用Dataset.window()
,看起来很有希望:
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]
但是,我想使用最后一个值作为目标。如果每个窗口都是张量,我会使用:
dataset = dataset.map(lambda window: (window[:-1], window[-1:]))
但是,如果我尝试这个,我会得到一个例外:
TypeError: '_VariantDataset' object is not subscriptable