Search code examples
tensorflowtfrecord

Parsing TFRecord when in eager execution


Considering that is possible to run tf.Data.Datasets in eager execution mode, how should I open a TFRecord file on eager execution? I'm more concerned about the parser writing, because I'm currently using dataset.make_one_shot_iterator as an iterator (between several images on my container).


Solution

  • In TensorFlow 1.8 onwards you can naturally iterate on the tf.data.Dataset object with eager execution enabled.

    ds = tf.data.TFRecordDataset(...).map(...).batch(...)
    for x in ds:
      print(x)
    

    make_one_shot_iterator will also work (kept working to keep compatible with equivalent code for graph construction):

    ds = tf.data.TFRecordDataset(...).map(...).batch(...)
    itr = ds.make_one_shot_iterator()
    for x in itr:
      print(x)
    

    However, in older versions of TensorFlow (where eager execution is a newly introduced feature and thus less baked), you'll have to wrap your dataset in a tf.contrib.eager.Iterator, using something like:

    tfe  tf.contrib.eager
    ds = tf.data.TFRecordDataset(...).map(...).batch(...)
    for x in tfe.Iterator(ds):
      print(x)
    

    See these resources associated with the 1.7 release: - Notebook - RNN example

    Hope that helps.