1

Here is toy code that duplicates a problem I am encountering when trying to generate/feed training data on the fly using a generator.

def makeRand():
    yield np.random.rand(1)

dataset = tf.data.Dataset.from_generator(makeRand, (tf.float32))

iterator = tf.contrib.data.Iterator.from_structure(tf.float32, tf.TensorShape([]))

next_x = iterator.get_next()

init_op = iterator.make_initializer(dataset)

with tf.Session() as sess:
    sess.run(init_op)
    a = sess.run(next_x)
    print(a)
    a = sess.run(next_x)
    print(a)

The trace looks like:

Traceback (most recent call last):
  File “test_iterator_gen.py", line 31, in <module>
    a = sess.run(next_x)
 tensorflow.python.framework.errors_impl.OutOfRangeError: End of sequence
     [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[]], output_types=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]

Caused by op 'IteratorGetNext', defined at:
  File "test_iterator_gen.py", line 23, in <module>
    next_x = iterator.get_next()
OutOfRangeError (see above for traceback): End of sequence
     [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[]], output_types=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]

1 Answer 1

1

This is caused by incorrect instantiation of a generator.

The error is caused by makeRand() running out of elements to yield. This is fixed by changing it to:

def makeRand():
   while True:
      yield np.random.rand(1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.