How can I make a Tensor Flow graph push an incrementing number to a queue?
I am just doing this for learning purposes, so I'd prefer if you kept it similar to what I'm doing (and correct what I'm doing wrong). This is my code:
import tensorflow as tf
# create queue
queue = tf.RandomShuffleQueue(capacity=10, min_after_dequeue=1, dtypes=tf.float32)
# create variables, and "add" operation
push_var = tf.Variable(initial_value=1.0, trainable=False)
add = push_var.assign_add(1)
# enqueue operation
push = queue.enqueue(add)
# dequeue operation
pop = queue.dequeue()
sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
# add var to stack
sess.run(push) # push_var = 2 after ran
sess.run(push) # push_var = 3 after ran
sess.run(push) # push_var = 4 after ran
sess.run(push) # push_var = 5 after ran
sess.run(push) # push_var = 6 after ran
sess.run(push) # push_var = 7 after ran
sess.run(push) # push_var = 8 after ran
# pop variable (random shuffle)
print sess.run(pop)
print sess.run(pop)
sess.close()
Output:
8
8
I'm expecting it to be 2 random numbers between 2 and 8. Instead, it always is popping the current value of the variable.
Is this because instead of pushing the actual value of the variable I am instead pushing a pointer to the variable? Tensor Flow's documentation says assign_add
returns
A Tensor that will hold the new value of this variable after the addition has completed.
Again, I'm trying to learn about Tensor Flow. I'd appreciate any learning resources (besides the TensorFlow website) if you have any! Thanks.
EDIT:
Changing push = queue.enqueue(add)
to push = queue.enqueue(add + 0)
results in expected behavior. Could someone explain this?