【问题标题】:Iterating over a Dataset TF 2.0 with for loop使用 for 循环迭代 Dataset TF 2.0
【发布时间】:2019-08-30 10:37:50
【问题描述】:

考虑到make_initializable_iterator() 已被弃用,这个问题是关于如何迭代 TF 数据集。

我用下面的函数读取了一个数据集:

def read_dataset_new(filename, target='delay'):
    ds = tf.data.TFRecordDataset(filename)
    ds = ds.map(lambda buf: parse(buf, target=target))
    ds = ds.batch(1)
    return ds

然后我想遍历数据集。我一直在使用: https://www.tensorflow.org/api_docs/python/tf/data/Dataset#make_initializable_iterator

with tf.compat.v1.Session() as sess:
    data_set = tfr_utils.read_dataset_new(self.tf_rcrds_fl_nm)
    itrtr = data_set.make_initializable_iterator()
    sess.run(itrtr.initializer)
    features, label = itrtr.get_next()
    features_keys = features.keys()
...

但是“警告:此功能已弃用。它将在未来的版本中删除。更新说明:用于...在数据集中:....”

除了弃用警告之外,我的代码按预期工作。

鉴于弃用警告,我现在正在尝试这个:

with tf.compat.v1.Session() as sess:
    data_set = tfr_utils.read_dataset_new(self.tf_rcrds_fl_nm)
    for features, label in data_set:
        features_keys = features.keys()
        ...

但这不起作用。我明白了:

self = <tensorflow.python.client.session.Session object at 0x12f2e57d0>
fn = <function BaseSession._do_run.<locals>._run_fn at 0x12f270440>
args = ({}, [<tensorflow.python.pywrap_tensorflow_internal.TF_Output; proxy of <Swig Object of type 'TF_Output *' at 0x12f3f75a0> >], [], None, None)
message = 'Resource AnonymousIterator/AnonymousIterator0/N10tensorflow4data16IteratorResourceE does not exist.\n\t [[node Iterat...tNext_1 (defined at /demo-routenet/tests/unit/test_tfrecord_utils.py:376) ]]'
m = <re.Match object; span=(102, 130), match='[[{{node IteratorGetNext_1}}'>

我能够找到的所有代码示例都显式地创建了一个迭代器,这显然不是一个应该做的事情。我找不到一个应该做什么的例子。

我怀疑有些东西没有被初始化。所以,我也试过了:

sess.run(data_set)

但这也不起作用(我也没有任何理由认为它应该有,但只是为了让你们都知道我尝试了什么)。

那么,如何按照弃用评论的建议在 for 循环中使用数据集?

【问题讨论】:

  • 阅读了关于 TF 2.0 的最新指南后,我现在了解到 Eager Execution 现在旨在成为默认模式。来自:tensorflow.org/guide/effective_tf2“TensorFlow 1.X 要求用户通过调用 tf.* API 手动将抽象语法树(图)拼接在一起。然后要求用户通过传递一组输出张量来手动编译抽象语法树并将张量输入到 session.run() 调用中。TensorFlow 2.0 会急切地执行(就像 Python 通常那样),而在 2.0 中,图形和会话应该感觉像是实现细节。”

标签: python tensorflow tensorflow-datasets


【解决方案1】:

目前还不是很清楚你想在你的输出中得到什么。 如果您想获取数据集输出的值,您应该急切地执行。 示例:

tf.compat.v1.enable_eager_execution()

def read_dataset_new(filename, target='delay'):
    ds = tf.data.TFRecordDataset(filename)
    ds = ds.map(lambda buf: parse(buf, target=target))
    ds = ds.batch(1)
    return ds
# This should return your key values for each example.
for features, labels in read_dataset_new(self.tf_rcrds_fl_nm):
    features_keys = features.keys()
# This should return your tensor values if they supposed to be numeric.
for features, labels in read_dataset_new(self.tf_rcrds_fl_nm):
    features_array = numpy.array(features)

【讨论】:

  • 谢谢。我有一个我的代码版本,可以按照您在上面github.com/Data-Science-Projects/demo-routenet/blob/master/… 的建议工作。该代码有效,因为我正在调用 tf.compat.v1.enable_eager_execution()。这并不代表我延迟执行图表以供稍后在会话中使用时的代码习惯用法。我将对此进行处理,看看是否有可以在会话中使用的代码版本。
  • API 版本 2 对 iter 有这样的注释:创建一个迭代器来枚举该数据集的元素。返回的迭代器实现了 Python 迭代器协议,因此只能在急切模式下使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 2012-06-27
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多