Skip to content Skip to sidebar Skip to footer

Tensorflow Fifoqueue Not Fifo?

I am enqueuing items in a particular order onto a TensorFlow FIFOQueue and expecting to be able to dequeue them in the same order but this is not the behaviour I am observing. Runn

Solution 1:

It appears tensorflow.python.ops.control_flow_ops.with_dependencies isn't working as I thought, or I was using it incorrectly. If I switch to to using tf.control_dependencies instead, I get the behaviour I need:

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 = Nonefor step_index inrange(step_count):
    step = tf.strided_slice(
        batch, [0, step_index * step_size],
        [tf.shape(batch)[0], (step_index + 1) * step_size])

    if dependency isNone:
        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 inrange(batch_count):
        for step_index inrange(step_count):
            print("Batch %d, step %d" % (batch_index, step_index))
            print(session.run(step))

This answer was motivated by the answer to another SO question.

Post a Comment for "Tensorflow Fifoqueue Not Fifo?"