Tensor Flow 中的入队和递增变量 [英] Enqueue and increment variable in Tensor Flow

查看:37
本文介绍了Tensor Flow 中的入队和递增变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使张量流图将递​​增的数字推送到队列?

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()

输出:

8
8

我希望它是 2 到 8 之间的 2 个随机数.相反,它总是弹出变量的当前值.

I'm expecting it to be 2 random numbers between 2 and 8. Instead, it always is popping the current value of the variable.

这是不是因为我没有推送变量的实际值,而是推送了一个指向变量的指针?张量流的文档assign_add 返回

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.

再次,我正在尝试了解张量流.如果您有任何学习资源(TensorFlow 网站除外),我将不胜感激!谢谢.

Again, I'm trying to learn about Tensor Flow. I'd appreciate any learning resources (besides the TensorFlow website) if you have any! Thanks.

push = queue.enqueue(add) 更改为 push = queue.enqueue(add + 0) 会导致预期的行为.有人能解释一下吗?

Changing push = queue.enqueue(add) to push = queue.enqueue(add + 0) results in expected behavior. Could someone explain this?

推荐答案

@David Wong 是正确的,变量只是对其底层张量的引用.即使你已经推送了 7 次,队列中的 7 个元素都指向同一个底层张量.当 pop 被执行时,底层张量被引用并返回.

@David Wong is correct that the variable is just a reference to its underlying tensor. Even though you've pushed it 7 times, the 7 elements in the queue all point to the same underlying tensor. When pop is executed, the underlying tensor is referenced and returned.

让我再解释一下.assign_add(1) 只是更新引用的值,因此它返回一个引用.当您执行 push = queue.enqueue(add) 时,它会在内部调用 tf.convert_to_tensor(add) 如果它的输入也是一个引用,它将返回一个引用.

Let me explain a little bit more. The assign_add(1) simply updates the referenced value, so it returns a reference. When you do push = queue.enqueue(add), it internally calls tf.convert_to_tensor(add) which would return a reference if its input is also a reference.

您可以在 python shell 中检查 tf.convert_to_tensor(add) 的输出:

You can inspect the output of tf.convert_to_tensor(add) in the python shell:

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

dtype=float32_ref 表示它是一个引用.

至于add + 0,也可以在ipython shell中查看,相当于tf.add(add, 0):

As for add + 0, you can also inspect it in the ipython shell, which is equivalent to tf.add(add, 0):

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

它不是引用并且有一个父节点add = push_var.assign_add(1).

It is not a reference and has a parent node add = push_var.assign_add(1).

所以这里的问题是

1) 当张量被推入队列时,它会被评估,它的所有父节点也会被评估.

1) a tensor would be evaluated when it is pushed to a queue, all its parent nodes would be evaluated as well.

在你的例子中,add + 0 被评估,所以它的父节点 add = push_assign_add(1) 将引用的值增加 1.

In your case, the add + 0 is evaluated, so is its parent node add = push_assign_add(1) which incremented the referenced value by 1.

2) 推入队列时不评估引用.队列中只有引用.当它们被弹出和引用时,它们的实际张量值被获取.

2) a reference is not evaluated when pushed to a queue. There are simply references in the queue. When they are popped and referenced, their actually tensor values are fetched.

就您而言,所有这些引用都指向同一个张量.所以弹出所有返回8.

In your case, all these references all point to the same tensor. So the pops all return 8.

这篇关于Tensor Flow 中的入队和递增变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆