0

This Tensorflow code below raises a 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()

Why is tf.errors.OutofRangeError printed when all the samples are read? It seems unreasonable.

benjaminplanche
  • 14,689
  • 5
  • 57
  • 69
刘米兰
  • 183
  • 2
  • 11

1 Answers1

0

From tf.errors.OutofRangeError doc:

Raised when an operation iterates past the valid input range.

This exception is raised in "end-of-file" conditions, such as when a tf.QueueBase.dequeue operation is blocked on an empty queue, and a tf.QueueBase.close operation executes.

I.e. it is a normal, python-esque behavior. You are iterating over your queue until it's empty; and you know it is when the OutofRangeError is thrown.

This also matches the behavior of normal python Queue:

import Queue

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

You can find a small discussion here on the advantages of this try-catch concept: Python: Queue.Empty Exception Handling.

benjaminplanche
  • 14,689
  • 5
  • 57
  • 69