【发布时间】:2021-12-19 09:15:49
【问题描述】:
我正在尝试为我的 LSTM 模型创建一个输入管道。我正在使用tf.data.Dataset.from_generator API 来做到这一点。在guide 之后,我当前的最小示例如下所示:
class generator:
def __init__(self, n=5):
self.n = n
def __call__(self):
for i in range(self.n):
yield (i, 10*i)
dataset = tf.data.Dataset.from_generator(generator(),
output_signature=(tf.TensorSpec(shape=(), dtype=tf.uint16), tf.TensorSpec(shape=(), dtype=tf.int32)))
window_size = 3
windows = dataset.window(window_size, shift=1)
def sub_to_batch(sub):
return sub.batch(window_size, drop_remainder=True)
final_dset = windows.flat_map(sub_to_batch)
print(list(final_dset.as_numpy_iterator()))
错误信息
TypeError: tf__sub_to_batch() takes 1 positional argument but 2 were given
只有在生成器中使用多个功能时才会出现此问题(例如更改以下行)。
yield (i)
dataset = tf.data.Dataset.from_generator(generator(),
output_signature=(tf.TensorSpec(shape=(), dtype=tf.uint16)))
在只有 1 个功能的版本中,输出类似于 shape=(3, 3, 1)
[ [ [0], [1], [2] ],
[ [1], [2], [3] ],
[ [2], [3], [4] ] ]
我基本上尝试实现单个功能的压缩,以便我得到shape=(3, 3, 2):
[ [ [0, 0], [1, 10], [2, 20] ],
[ [1, 10], [2, 20], [3, 30] ],
[ [2, 20], [3, 30], [4, 40] ] ]
如何做到这一点?
【问题讨论】:
标签: python tensorflow time-series lstm tensorflow-datasets