tf.identity的一些问题

Alienge 2017-09-02 10:37:33
我在测试这段代码的时候,输出的结果不是一个唯一值,运行一次就会出现一个结果
import tensorflow as tf

x = tf.Variable(0, dtype=tf.int32)
old_val = tf.identity(x)
old_val = old_val+10
new_val = tf.assign(x,x+1)

with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for i in range(3):
print(sess.run([old_val,new_val, x]))


结果为
[11, 1, 1]
[11, 2, 2]
[12, 3, 3]
或者
[10, 1, 1]
[11, 2, 2]
[12, 3, 3]
或者
[10, 1, 1]
[12, 2, 2]
[12, 3, 3]
我猜测是不是跟
old_val = tf.identity(x)

在计算图中执行的顺序有关,我请教一下 tf.identity(x)的具体用法
...全文
1779 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndersonTyron 2019-09-26
  • 打赏
  • 举报
回复
Hi, ELI_CJ. This maybe cause by dependencies missing in your code. so, when running, the system did not know which one of ops of [old_val,new_val, x] was to be evaluated in priority. To oder variables in calculation, use tf.control_dependencies. Following is just a slight improvement on your code:
x=tf.Variable(0,dtype=tf.int32)
with tf.control_dependencies([x]):
old_val = tf.identity(x)
with tf.control_dependencies([old_val]):
old_val = tf.identity(old_val+10)
with tf.control_dependencies([x,old_val]):
new_val_op=tf.assign(x,x+1)
with tf.control_dependencies([new_val_op]):
new_val=tf.identity(new_val_op)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(3):
print(sess.run([old_val, new_val,x]))

my results are
[10, 1, 1]
[11, 2, 2]
[12, 3, 3]

Not surprised, one can found that a liner increase appears in the first column is particularly because x = x+1 has been performed in tf.assign(x, x+1).
Alienge 2017-11-20
  • 打赏
  • 举报
回复
Thank you
ELI_CJ 2017-10-26
  • 打赏
  • 举报
回复
tf.identity is useful when you want to explicitly transport tensor between devices (like, from GPU to a CPU). The op adds send/recv nodes to the graph, which make a copy when the devices of the input and the output are different. A default behavior is that the send/recv nodes are added implicitly when the operation happens on a different device but you can imagine some situations (especially in a multi-threaded/distributed settings) when it might be useful to fetch the value of the variable multiple times within a single execution of the session.run. tf.identity allows for more control with regard to when the value should be read from the source device. Possibly a more appropriate name for this op would be read. Also, please note that in the implementation of tf.Variable link, the identity op is added in the constructor, which makes sure that all the accesses to the variable copy the data from the source only once. Multiple copies can be expensive in cases when the variable lives on a GPU but it is read by multiple CPU ops (or the other way around). Users can change the behavior with multiple calls to tf.identity when desired. EDIT: Updated answer after the question was edited. In addition, tf.identity can be used used as a dummy node to update a reference to the tensor. This is useful with various control flow ops. In the CIFAR case we want to enforce that the ExponentialMovingAverageOp will update relevant variables before retrieving the value of the loss. This can be implemented as: with tf.control_dependencies([loss_averages_op]): total_loss = tf.identity(total_loss) Here, the tf.identity doesn't do anything useful aside of marking the total_loss tensor to be ran after evaluating loss_averages_op.
ELI_CJ 2017-10-26
  • 打赏
  • 举报
回复
tf.identity(input, name=None) Return a tensor with the same shape and contents as the input tensor or value. Args: input: A Tensor. name: A name for the operation (optional). Returns: A Tensor. Has the same type as input.

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧