Enqueue and increment variable in Tensor Flow

How can I schedule Tensor Flow to push an increasing number into a queue?

I just do this for training purposes, so I would prefer that you are similar to what I am doing (and correcting what I am 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()

Conclusion:

8
8

I expect it to be 2 random numbers between 2 and 8. Instead, it always gives the current value of the variable.

Is it because instead of clicking on the actual value of the variable, I instead click on the variable pointer? Tensor flow documentation says assign_addreturns

A tensor that will hold the new value of this variable after the addition is complete.

, Tensor Flow. ( - TensorFlow), ! .

EDIT:

push = queue.enqueue(add) push = queue.enqueue(add + 0) . - ?

+4
2

@ , . , 7 , 7 . pop , .

. assign_add(1) , . push = queue.enqueue(add), tf.convert_to_tensor(add), , .

tf.convert_to_tensor(add) python:

In [2]: tf.convert_to_tensor(add)
Out[2]: <tf.Tensor 'AssignAdd:0' shape=() dtype=float32_ref>

dtype=float32_ref , .

add + 0, ipython, tf.add(add, 0):

In [3]: add+0
Out[3]: <tf.Tensor 'add:0' shape=() dtype=float32>

node add = push_var.assign_add(1).

,

1) , , .

add + 0, node add = push_assign_add(1), 1.

2) . . , .

. , 8.

+2

, "" push_var. , , . "add + 0" , , "add + 0" "add" , .

+1

Source: https://habr.com/ru/post/1661132/


All Articles