用for循环迭代TF 2.0数据集

2024-09-28 23:46:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题是关于在不推荐使用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

^{pr2}$

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

除了不推荐警告之外,我的代码按预期工作。在

不过,考虑到不推荐警告,我现在正在尝试:

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循环中使用数据集呢?正如反对意见所建议的那样?在


Tags: 数据run警告targetdatamaketftensorflow
1条回答
网友
1楼 · 发布于 2024-09-28 23:46:17

你不太清楚你想要在你的输出中得到什么。 如果你想得到数据集输出的值,你应该立即执行。 示例:

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)

相关问题 更多 >