我正在尝试创建一个数据集,它将从时间序列中返回随机窗口,并将下一个值作为目标,使用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
flat_map
会失去len()
的能力,但是可以使用较慢的len(list(dataset))
版本。 - Harbor