How to iterate over a tf.dataset, without deprecated functions?

Viewed 2675

I'm using tensorflow 1.14 and have a problem with dataset.

my code:

my_data = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7]
]

slices = tf.data.Dataset.from_tensor_slices(my_data) # get dataset
it = slices.make_one_shot_iterator() # get iterator from dataset (deprecated)
next_item = it.get_next()

It says make_one_shot_iterator is deprecated..

So i tried following codes

my_data = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7]
]

slices = tf.data.Dataset.from_tensor_slices(my_data) # get dataset
for q in slices:
    print(sess.run(q))

Immediately i got NotFoundError exception.

My question: What is the proper way to iterate over a dataset?

3 Answers

From the documentation of tf.data.Dataset you can do a simple loop with:

for element in my_dataset: 
   print(element)

As you can see in the image, this returns a tf.Tensor. If you want a simple tuple you can use:

for element in my_dataset.as_numpy_iterator(): 
   print(element)

If each entry of your dataset has more than one element you can index the contents of the tuple with [] like normally.

enter image description here

Try this:

import tensorflow as tf
my_data = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7]
]
n = len(my_data)
slices = tf.data.Dataset.from_tensor_slices(my_data) # get dataset
iterator = slices.make_initializable_iterator()

with tf.Session() as sess:
    sess.run(iterator.initializer)
    while n>0:
        print(sess.run(iterator.get_next()))
        n-=1

If above still showing deprecation message, then try below code:

import tensorflow as tf
tf.enable_eager_execution()

my_data = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7]
]
slices = tf.data.Dataset.from_tensor_slices(my_data) # get dataset
for i in slices:
    print(i.numpy())

output:

[0 1]
[2 3]
[4 5]
[6 7]
import tensorflow as tf
my_data = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7]
]

slices = tf.data.Dataset.from_tensor_slices(my_data) # get dataset
q = slices.make_one_shot_iterator().get_next()
with tf.Session() as sess:
    for i in range(len(my_data)):
        print('-----')
        print(sess.run(q))

The code above produces enter image description here

Related