TensorFlow 数据集 API from_generator 序列结束错误

TensorFlow Dataset API from_generator End of Sequence Error

这是重复我在尝试使用生成器动态generate/feed训练数据时遇到的问题的玩具代码。

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)

轨迹看起来像:

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)]]

这是由于生成器实例化不正确造成的。

错误是由 makeRand() 运行 out of elements to yield 引起的。这是通过将其更改为来解决的:

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