简体   繁体   English

建议在 tensorflow 2.0 中调试 `tf.data.Dataset` 操作

[英]advise for debugging `tf.data.Dataset` operations in tensorflow 2.0

What is the equivalent of Panda's df.head() for tf datasets? tf 数据集的 Panda df.head()相当于什么?

Following the documentation here I've constructed the following toy examples:按照此处的文档我构建了以下玩具示例:

dset = tf.data.Dataset.from_tensor_slices((tf.constant([1.,2.,3.]), tf.constant([4.,4.,4.]), tf.constant([5.,6.,7.])))
print(dset)

outputs产出

<TensorSliceDataset shapes: ((), (), ()), types: (tf.float32, tf.float32, tf.float32)>

I would prefer to get back something resembling a tensor , so to get some values I'll make an iterator.我更喜欢找回类似于 tensor 的东西,所以为了获得一些值,我将制作一个迭代器。

dset_iter = dset.__iter__()
print(dset_iter.next())

outputs产出

(<tf.Tensor: id=122, shape=(), dtype=float32, numpy=1.0>,
 <tf.Tensor: id=123, shape=(), dtype=float32, numpy=4.0>,
 <tf.Tensor: id=124, shape=(), dtype=float32, numpy=5.0>)

So far so good.到现在为止还挺好。 Let's try some windowing...让我们尝试一些窗口...

windowed = dset.window(2)
print(windowed)

outputs产出

<WindowDataset shapes: (<tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b25c0>, <tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b27b8>, <tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b29b0>), types: (<tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b25c0>, <tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b27b8>, <tensorflow.python.data.ops.dataset_ops.DatasetStructure object at 0x1349b29b0>)>

Ok, use the iterator trick again:好的,再次使用迭代器技巧:

windowed_iter = windowed.__iter__()
windowed_iter.next()

outputs产出

(<_VariantDataset shapes: (), types: tf.float32>,
 <_VariantDataset shapes: (), types: tf.float32>,
 <_VariantDataset shapes: (), types: tf.float32>)

What?什么? A WindowDataset 's iterator gives back a tuple of other dataset objects? WindowDataset的迭代器返回其他数据集对象的元组
I would expect the first item in this WindowDataset to be the tensor with values [[1.,4.,5.],[2.,4.,6.]].我希望此 WindowDataset 中的第一项是值为 [[1.,4.,5.],[2.,4.,6.]] 的张量。 Maybe this is still true, but it isn't readily apparent to me from this 3-tuple of datasets.也许这仍然是正确的,但从这个 3 元数据集对我来说并不容易看出。 Ok.好的。 Let's get their iterators...让我们得到他们的迭代器...

vd = windowed_iter.get_next()
vd0, vd1, vd2 = vd[0], vd[1], vd[2]
vd0i, vd1i, vd2i = vd0.__iter__(), vd1.__iter__(), vd2.__iter__()
print(vd0i.next(), vd1i.next(), vd2i.next())

outputs产出

(<tf.Tensor: id=357, shape=(), dtype=float32, numpy=1.0>,
 <tf.Tensor: id=358, shape=(), dtype=float32, numpy=4.0>,
 <tf.Tensor: id=359, shape=(), dtype=float32, numpy=5.0>)

As you can see, this workflow is quickly becoming a mess.如您所见,此工作流程很快变得一团糟。 I like how Tf2.0 is attempting to make the framework more interactive and pythonic.我喜欢 Tf2.0 尝试使框架更具交互性和 Pythonic 的方式。 Are there good examples of the datasets api conforming to this vision too?是否也有符合这一愿景的数据集 api 的好例子?

I was in a similar situation.我处于类似的情况。 I eventually ended up using zip我最终使用了zip

train_dataset = train_dataset.window(10, shift=5)
for step_dataset in train_dataset:
    for (images, labels, paths) in zip(*step_dataset):
        train_step(images, labels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM