为什么这个Tensorflow代码会引发tf.errors.OutOfRangeError?

时间:2018-06-20 10:13:59

标签: python tensorflow queue outofrangeexception

下面的这个Tensorflow代码引发一个tf.errors.OutofRangeError

try:
     while not coord.should_stop():        
          vector1,vector2,vector3,vector4,vector5,labels = sess.run([train_vector1,train_vector2,train_vector3,train_vector4,train_vector5,train_labels])
          shape1 = tf.shape(vector1)
          print (sess.run(shape1))
except tf.errors.OutOfRangeError:
   print ('tf.errors.OutOfRangeError')

finally:
    coord.request_stop()

读取所有样本后为什么打印tf.errors.OutofRangeError? 似乎不合理。

1 个答案:

答案 0 :(得分:0)

来自tf.errors.OutofRangeError文档:

  

当操作迭代超出有效输入范围时引发。

     

在“文件结束”条件下(例如当   tf.QueueBase.dequeue操作在空队列中被阻止,并且   tf.QueueBase.close操作执行。

即这是正常的,类似python的行为。您正在遍历队列,直到队列为空。而且您知道是在抛出OutofRangeError的时候。

这也符合普通python Queue的行为:

import Queue

q = Queue.Queue()
try:
    task=q.get(False)
    # ...
except Queue.Empty:
    # Handle empty queue here
    pass

您可以在这里找到有关尝试捕获概念的优点的小讨论:Python: Queue.Empty Exception Handling