2017-08-17 80 views
1

FIFO Queue將在其enqueuedequeue功能暫停時分別排隊是滿或空。當你只有一個隊列時,這不是問題。如何防止FIFO隊列暫停有條件

我的情況是我有兩個隊列,我有以下一些條件入隊:

run_options = tf.RunOptions(timeout_in_ms=10000) 
i1, l1, i2, l2 = produce_sample() 

if l1 == l2: 
    sess.run(enqueue_same_op, feed_dict={x1: i1, y1: l1, x2: i2, y2: l2}, \ 
      options=run_options) 
else: 
    sess.run(enqueue_diff_op, feed_dict={x1: i1, y1: l1, x2: i2, y2: l2}) 

enqueue_same_openqueue_diff_op是分別queue_samequeue_diff操作。

因爲l1<>l2很多的概率較大l1==l2。因此,即使我採用了run_options,會議也會暫停排隊queue_diff

有沒有辦法讓獲得隊列的大小測試如果它已滿?

非常感謝您提前。

回答

0

有沒有辦法獲得隊列的大小,或測試它是否已滿?

您可以使用size()功能和測試針對隊列容量

capacity=1000 # your queue capacity 
dtype=tf.float32 
queue_diff = tf.FIFOQueue(capacity, dtype) 
enqueue_op = tf.cond(tf.equal(queue_diff.size(), capacity), lambda:is_full(), lambda: enqueue(queue_diff, enqueue_elements..)) 

# a function to takecare when the queue is full 
def is_full(): 
    return 'Enqueue_Failed' 

# a function for enqueue ops 
def enqueue(queue, element...): 
    queue.enqueue(element) 
    return 'Enqueue_Success' 
0

您可以通過調用size方法,它返回一個張量得到一個隊列的大小設置隊列的大小:

$ queue = tf.FIFOQueue(capacity=100, dtype=tf.float32) 
$ queue.size() 
<tf.Tensor 'fifo_queue_Size:0' shape=() dtype=int32> 

如果有一個固定的大小的隊列,在像上述的示例,可以使用調節功能tf.cond的流量,從而判斷是否滿了。

或者,您可以使用capacity=-1使您的隊列不受限制。嚴格地說,這不是開放API和官方文件中沒有說明,但可以在源代碼中找到它:

def _fifo_queue_v2(component_types, shapes=None, capacity=None, 
        container=None, shared_name=None, name=None): 
    r"""A queue that produces elements in first-in first-out order. 

    Args: 
    component_types: A list of `tf.DTypes` that has length `>= 1`. 
     The type of each component in a value. 
    shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`. 
     The shape of each component in a value. The length of this attr must 
     be either 0 or the same as the length of component_types. If the length of 
     this attr is 0, the shapes of queue elements are not constrained, and 
     only one element may be dequeued at a time. 
    capacity: An optional `int`. Defaults to `-1`. 
     The upper bound on the number of elements in this queue. 
     Negative numbers mean no limit. 
    container: An optional `string`. Defaults to `""`. 
     If non-empty, this queue is placed in the given container. 
     Otherwise, a default container is used. 
    shared_name: An optional `string`. Defaults to `""`. 
     If non-empty, this queue will be shared under the given name 
     across multiple sessions. 
    name: A name for the operation (optional). 

    Returns: 
    A `Tensor` of type `resource`. The handle to the queue. 
    """ 
    result = _op_def_lib.apply_op("FIFOQueueV2", 
           component_types=component_types, 
           shapes=shapes, capacity=capacity, 
           container=container, shared_name=shared_name, 
           name=name) 
    return result 

如果您熟悉了這種依賴,你一定隊列贏得不會吃掉所有可用內存,您可以通過-1,從而大大簡化您的代碼。