Python TensorFlow FIFO不是FIFO?
我将项目按特定顺序排到TensorFlow上,并希望能够以相同的顺序将它们排出来,但这不是我观察到的行为 运行以下独立代码将演示该方法和行为。这已经在Python2.7(但可能在Python3中运行)和TensorFlow 1.1上运行Python TensorFlow FIFO不是FIFO?,python,tensorflow,Python,Tensorflow,我将项目按特定顺序排到TensorFlow上,并希望能够以相同的顺序将它们排出来,但这不是我观察到的行为 运行以下独立代码将演示该方法和行为。这已经在Python2.7(但可能在Python3中运行)和TensorFlow 1.1上运行 from __future__ import division, print_function, unicode_literals import math import numpy import tensorflow as tf from tensorflow.
from __future__ import division, print_function, unicode_literals
import math
import numpy
import tensorflow as tf
from tensorflow.python.training import queue_runner
from tensorflow.python.ops import control_flow_ops
row_count, column_count = 7, 5
batch_size, step_size = 3, 2
# Create some random data
data = numpy.arange(row_count * column_count).reshape(
(row_count, column_count))
print(data)
batch_count = int(math.ceil(row_count / batch_size))
step_count = int(math.ceil(column_count / step_size))
print(batch_count, step_count)
slices = tf.train.slice_input_producer([data], num_epochs=1, shuffle=False)
batch = tf.train.batch(slices, batch_size, allow_smaller_final_batch=True)
queue = tf.FIFOQueue(32, dtypes=[batch.dtype])
enqueue_ops = []
dependency = None
for step_index in range(step_count):
step = tf.strided_slice(
batch, [0, step_index * step_size],
[tf.shape(batch)[0], (step_index + 1) * step_size])
if dependency is None:
dependency = step
else:
step = control_flow_ops.with_dependencies([dependency], step)
enqueue_ops.append(queue.enqueue(step))
queue_runner.add_queue_runner(queue_runner.QueueRunner(
queue=queue, enqueue_ops=[tf.group(*enqueue_ops)]))
step = queue.dequeue()
supervisor = tf.train.Supervisor()
with supervisor.managed_session() as session:
for batch_index in range(batch_count):
for step_index in range(step_count):
print("Batch %d, step %d" % (batch_index, step_index))
print(session.run(step))
预期产量为
Batch 0, step 0
[[ 0 1]
[ 5 6]
[10 11]]
Batch 0, step 1
[[ 2 3]
[ 7 8]
[12 13]]
Batch 0, step 2
[[ 4]
[ 9]
[14]]
Batch 1, step 0
[[15 16]
[20 21]
[25 26]]
Batch 1, step 1
[[17 18]
[22 23]
[27 28]]
Batch 1, step 2
[[19]
[24]
[29]]
Batch 2, step 0
[[30 31]]
Batch 2, step 1
[[32 33]]
Batch 2, step 2
[[34]]
Batch 0, step 0
[[ 0 1]
[ 5 6]
[10 11]]
Batch 0, step 1
[[ 4]
[ 9]
[14]]
Batch 0, step 2
[[ 2 3]
[ 7 8]
[12 13]]
Batch 1, step 0
[[15 16]
[20 21]
[25 26]]
Batch 1, step 1
[[19]
[24]
[29]]
Batch 1, step 2
[[17 18]
[22 23]
[27 28]]
Batch 2, step 0
[[30 31]]
Batch 2, step 1
[[32 33]]
Batch 2, step 2
[[34]]
实际输出为
Batch 0, step 0
[[ 0 1]
[ 5 6]
[10 11]]
Batch 0, step 1
[[ 2 3]
[ 7 8]
[12 13]]
Batch 0, step 2
[[ 4]
[ 9]
[14]]
Batch 1, step 0
[[15 16]
[20 21]
[25 26]]
Batch 1, step 1
[[17 18]
[22 23]
[27 28]]
Batch 1, step 2
[[19]
[24]
[29]]
Batch 2, step 0
[[30 31]]
Batch 2, step 1
[[32 33]]
Batch 2, step 2
[[34]]
Batch 0, step 0
[[ 0 1]
[ 5 6]
[10 11]]
Batch 0, step 1
[[ 4]
[ 9]
[14]]
Batch 0, step 2
[[ 2 3]
[ 7 8]
[12 13]]
Batch 1, step 0
[[15 16]
[20 21]
[25 26]]
Batch 1, step 1
[[19]
[24]
[29]]
Batch 1, step 2
[[17 18]
[22 23]
[27 28]]
Batch 2, step 0
[[30 31]]
Batch 2, step 1
[[32 33]]
Batch 2, step 2
[[34]]
请注意,批次0和批次1中的步骤顺序不正确。我无法确定步骤的顺序。看起来批次总是有序的,但每个批次中的步骤都是“随机”顺序:看起来是确定性的,但不是FIFO
我尝试过使用和不使用上述代码中使用的显式依赖项声明。我已尝试将队列容量设置为1。我尝试设置enqueue_ops=enqueue_ops
而不是使用tf.group
,但这些更改都没有帮助,最后一个更改导致了非常奇怪的输出
可能会忽略依赖项?看起来
tensorflow.python.ops.control\u flow\u ops.with\u dependencies
没有像我想的那样工作,或者我使用了不正确的方法。如果我转而使用tf.control\u dependencies
,我将获得所需的行为:
from __future__ import division, print_function, unicode_literals
import math
import numpy
import tensorflow as tf
from tensorflow.python.training import queue_runner
row_count, column_count = 7, 5
batch_size, step_size = 3, 2
# Create some random data
data = numpy.arange(row_count * column_count).reshape(
(row_count, column_count))
print(data)
batch_count = int(math.ceil(row_count / batch_size))
step_count = int(math.ceil(column_count / step_size))
print(batch_count, step_count)
slices = tf.train.slice_input_producer([data], num_epochs=1, shuffle=False)
batch = tf.train.batch(slices, batch_size, allow_smaller_final_batch=True)
queue = tf.FIFOQueue(32, dtypes=[batch.dtype])
enqueue_ops = []
dependency = None
for step_index in range(step_count):
step = tf.strided_slice(
batch, [0, step_index * step_size],
[tf.shape(batch)[0], (step_index + 1) * step_size])
if dependency is None:
dependency = queue.enqueue(step)
else:
with tf.control_dependencies([dependency]):
step = queue.enqueue(step)
dependency = step
enqueue_ops.append(step)
queue_runner.add_queue_runner(queue_runner.QueueRunner(
queue=queue, enqueue_ops=[tf.group(*enqueue_ops)]))
step = queue.dequeue()
supervisor = tf.train.Supervisor()
with supervisor.managed_session() as session:
for batch_index in range(batch_count):
for step_index in range(step_count):
print("Batch %d, step %d" % (batch_index, step_index))
print(session.run(step))
这一回答是出于以下原因