【问题标题】:TensorFlow Dataset API from_generator End of Sequence ErrorTensorFlow 数据集 API from_generator 序列结束错误
【发布时间】:2017-11-27 20:15:32
【问题描述】:

这是一个玩具代码,它复制了我在尝试使用生成器动态生成/提供训练数据时遇到的问题。

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

【问题讨论】:

    标签: python machine-learning tensorflow tensorflow-datasets


    【解决方案1】:

    这是由生成器的不正确实例化引起的。

    该错误是由于 makeRand() 没有足够的元素导致的。通过将其更改为:

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

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多