问题
This problem is about how to iterate over a TF Dataset given that make_initializable_iterator()
is deprecated.
I read a data set with the function below:
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
Then I want to iterate over the data set. I have been using: 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()
...
But "Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use for ... in dataset:...."
Apart from the deprecation warning, my code works as expected.
Given the deprecation warning, though, I am now trying this:
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()
...
But that does not work. I get:
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}}'>
The code samples I have been able to find all explicitly create an iterator, which is apparently not what one is supposed to do. I can't find an example of what one is supposed to do though.
I suspect that something has not been initialised. So, I also tried:
sess.run(data_set)
But that didn't work either (nor do I have any reason to suppose it should have, but just so you all know what I tried).
So, how does one use a Dataset in a for loop as the deprecation comment suggests please?
回答1:
It is not very clear what you want to get at your output. If you want to get the values of the dataset output you should execute eagerly. Example:
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)
来源:https://stackoverflow.com/questions/57725172/iterating-over-a-dataset-tf-2-0-with-for-loop