TensorFlow Dataset API from_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)

错误追踪如下:

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() 函数耗尽可yield元素引起的。通过将其更改为以下代码可以解决这个问题:

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注