Tensorflow:出队然后入队 [英] Tensorflow: Dequeue and then enqueue
问题描述
我有一个队列(称为 queue_A
)并在其中填充 100 个元素.如果我想做以下两件事:
I have a queue (called queue_A
) and populate 100 elements inside. If I would like to do the following 2 things:
- 从
queue_A
中取出 1 个元素,对其进行一些处理并将结果放入另一个队列(queue_B
)中.入队操作称为op_B
. - 将此元素(在处理之前)排回
queue_A
,这个入队操作称为op_A
.
- Dequeue 1 element from
queue_A
, do some processing on it and enqueue the result into another queue (queue_B
). The enqueuing op is calledop_B
. - Enqueue this element (before processing) back to
queue_A
, and this enqueuing op is calledop_A
.
为了达到 1,我可以这样写:
For achieving 1, I can write:
anElement = queue_A.dequeue()
result = proc(anElement)
op_B = queue_B.enqueue(result)
queue_runner = tf.train.QueueRunner(queue_B,
[op_B] * 4)
为了实现 2,我可以写:
For achieving 2, I can write:
anElement = queue_A.dequeue()
op_A = queue_A.enqueue(anElement)
queue_runner = tf.train.QueueRunner(queue_A,
[op_A] * 4)
但是,我不知道我怎么能同时做这两件事.现在,我使用以下代码:
However, I don't know how can I do these two things at once. Now, I use the following code:
anElement = queue_A.dequeue()
op_A = queue_A.enqueue(anElement)
result = proc(anElement)
op_B = queue_B.enqueue(result)
queue_runner = tf.train.QueueRunner(queue_B,
[op_A, op_B] * 4)
我期望 queue_A
的大小是一个常数,但是当我使用 session.run(queue_A.size())
检查它时,大小逐渐减小.那个代码有什么问题?以及如何实现我想要的?
I expect the size of queue_A
is a constant, but when I use session.run(queue_A.size())
to check it, the size is gradually decreasing.
What is wrong with that code? And how to achieve what I want?
推荐答案
您示例中的代码有两种类型的队列运行器":
The code in your example has two types of "queue runner":
- 一个运行
op_A
的:它从queue_A
中取出一个元素,并将它排入queue_B
. - 另一个运行
op_B
:它从queue_A
中取出一个元素,通过proc()
处理它,并将结果排入queue_A
代码>queue_B.
- One that runs
op_A
: it dequeues an element fromqueue_A
, and enqueues it back toqueue_B
. - Another that runs
op_B
: it dequeues an element fromqueue_A
, processes it viaproc()
, and enqueues the result back toqueue_B
.
问题在于,当 op_A
和 op_B
分别运行时(例如,在不同的队列运行器中,或在对 sess.run() 的不同调用中
code>),它们将从 queue_A
中取出 distinct 元素.运行op_B
出队的元素永远不会重新入队到queue_A
,这就解释了为什么它的大小会逐渐减小.
The problem is that, when op_A
and op_B
run separately (e.g. in different queue runners, or in different calls to sess.run()
), they will dequeue distinct elements from queue_A
. The elements dequeued by running op_B
will never be re-enqueued to queue_A
, which explains why its size gradually decreases.
为了解决这个问题,正如 Andrei 建议的,您需要创建一个运行单个 TensorFlow 子图的操作,该子图执行op_A
和 op_B
.以下示例应该可以工作:
To solve this problem, as Andrei suggests, you need to create an op that runs a single TensorFlow subgraph that performs both op_A
and op_B
. The following example should work:
anElement = queue_A.dequeue()
op_A = queue_A.enqueue(anElement)
result = proc(anElement)
op_B = queue_B.enqueue(result)
# Creates a single op that enqueues the original element back to queue_A and the
# processed element to queue_B.
op = tf.group(op_A, op_B)
queue_runner = tf.train.QueueRunner(queue_B, [op] * 4)
这篇关于Tensorflow:出队然后入队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!