tf.identity的意义以及用例
2017-11-09 23:22
585 查看
最近在学习tensorflow,学到ExponentialMovingAverage时,里面有一个tf.identity操作,在Stack Overflow上看到一个很好的解释,记录一下。
原地址 : https://stackoverflow.com/questions/34877523/in-tensorflow-what-is-tf-identity-used-for
下面程序要做的是,5次循环,每次循环给x加1,赋值给y,然后打印出来
这个打印的是0,0,0,0,0 。也就是说没有达到我们预期的效果
如果改成这样:
这时候打印的是1,2,3,4,5
解释:
tf.identity是返回了一个一模一样新的tensor,再control_dependencies的作用块下,需要增加一个新节点到gragh中。有待更新。。。
原地址 : https://stackoverflow.com/questions/34877523/in-tensorflow-what-is-tf-identity-used-for
下面程序要做的是,5次循环,每次循环给x加1,赋值给y,然后打印出来
x = tf.Variable(0.0) #返回一个op,表示给变量x加1的操作 x_plus_1 = tf.assign_add(x, 1) #control_dependencies的意义是,在执行with包含的内容(在这里就是 y = x)前 #先执行control_dependencies中的内容(在这里就是 x_plus_1) with tf.control_dependencies([x_plus_1]): y = x init = tf.initialize_all_variables() with tf.Session() as session: init.run() for i in xrange(5): print(y.eval())#相当于sess.run(y),由于control_dependencies的所以执行print前都会先执行x_plus_1
这个打印的是0,0,0,0,0 。也就是说没有达到我们预期的效果
如果改成这样:
x = tf.Variable(0.0) x_plus_1 = tf.assign_add(x, 1) with tf.control_dependencies([x_plus_1]): y = tf.identity(x)#修改部分 init = tf.initialize_all_variables() with tf.Session() as session: init.run() for i in xrange(5): print(y.eval()) This works: it prints 1, 2, 3, 4, 5.
这时候打印的是1,2,3,4,5
解释:
tf.identity是返回了一个一模一样新的tensor,再control_dependencies的作用块下,需要增加一个新节点到gragh中。有待更新。。。
相关文章推荐
- tf.identity的意义以及用例
- register_chrdev_region()参数意义以及动态分配设备号
- QEMU-KVM中关键的入口代码以及意义(不断更新中)
- Matlab中fminunc函数的意义 以及options函数的初级用法。
- 为什么读博士以及有什么意义
- 面向对象出现原因以及意义
- Matlab中fminunc函数的意义以及options函数的初级用法。
- tf.argmax()以及axis解析
- 立体匹配的研究背景以及意义
- 测试用例格式以及写作要点
- 关于C++中 map 的意义以及用法
- Linux下各个目录的意义以及一些常用指令
- STM32 BOOT设置以及对应的意义
- linux下查看日志文件 以及日志的意义
- listen函数里面backlog的意义以及各种情况
- 构造函数、getter和setter方法以及后两者的意义
- Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义
- ibatis学习(一)--ibatis介绍以及用例
- REST:Rails 1.2的意义,以及一个翻译接力的发起
- 继续做用例图,逐步清晰用例图的意义