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

标签 python machine-learning tensorflow tensorflow-datasets

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

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() 耗尽了要产生的元素而引起的。通过将其更改为以下内容可以解决此问题:

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

关于python - TensorFlow 数据集 API from_generator 序列结束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519104/

相关文章:

python - 'helloworld.pyx' 不匹配任何文件

python - 获取 Spark RDD 中每个键的最大值

python - 日期转换为 <class 'pandas._libs.tslibs.timestamps.Timestamp' > 而不是 datetime.datetime

python - Keras 中 Conv1D 的数据形状问题

python - 如何在 tensorflow 中对张量的列执行相似函数

tensorflow - 将变量从一个 TensorFlow 图复制到另一个

python - 我想实现从Windows到Linux的Popen代码:

c++ - std::function 有性能问题,如何避免?

machine-learning - 如何将NLP问题转换为知识图谱三元组?

python - tensorflow 、Python : ImportError: undefined symbol: _PyUnicode_AsWideCharString