In tensorflow, what should we do if we want run something before fetching data (such as, using queue in tensorflow)? Here is an example tested by myself:
import tensorflow as tf
from tensorflow.python.framework import ops
def my_func(x):
print("hello")
return x
queue = tf.FIFOQueue(10, "float32")
init = queue.enqueue_many(([1, 2, 3],))
my_op = tf.py_func(my_func, [1.0], tf.float32)
with ops.control_dependencies([my_op]):
inc = queue.enqueue([1],)
with tf.Session() as sess:
sess.run(init)
print("Before Enqueue")
sess.run(inc)
print("After Enqueue")
It will print
Before Enqueue hello After Enqueue
Successfully, we add an operation before enqueue a item into queue.