An unusually high number of `Iterator.get_next()` calls was detected

问题描述

./EXAMPLE.py:407: UserWarning: An unusually high number of `Iterator.get_next()` calls was detected.
    This often indicates that `Iterator.get_next()` is being called inside a training loop, which will cause gradual slowdown and eventual resource exhaustion. 
    If this is the case, restructure your code to call `next_element = iterator.get_next()` once outside the loop, and use `next_element` as the input to some computation that is invoked inside the loop.
  warnings.warn(GET_NEXT_CALL_WARNING_MESSAGE)
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread constructor failed: Resource temporarily unavailable

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

查看代码, 的确是将get_next放在了loop里面.

iterator = dataset.make_one_shot_iterator()
        while True:
            try:
                features, labels = iterator.get_next()
                features_val = sess.run(features)
                labels_val = sess.run(labels)
                features_val = np.array(features_val).reshape((32,28*28))
                labels_val = np.array(labels_val).reshape((32,1))
                sess.run(fetches=self.train_op, feed_dict={self.x: features_val, self.y: labels_val})

            except tf.errors.OutOfRangeError as _e:
                break

改正之后就正确了.

iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
        while True:
            try:
                features_val = sess.run(features)
                labels_val = sess.run(labels)
                features_val = np.array(features_val).reshape((32,28*28))
                labels_val = np.array(labels_val).reshape((32,1))
                sess.run(fetches=self.train_op, feed_dict={self.x: features_val, self.y: labels_val})

            except tf.errors.OutOfRangeError as _e:
                break

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据