最近在学习tensorflow,学到滑动平均窗口函数ExponentialMovingAverage时,里面用到了tf.identity操作,在Stack Overflow上看到一个很好的解释,记录一下,对其中程序稍微修改一下能更好地说明。
原地址 : https://stackoverflow.com/questions//in-tensorflow-what-is-tf-identity-used-for
下面程序的功能是,做5次循环,每次循环给x加1,赋值给y,然后打印出来,所以我们预期达到的效果是输出2,3,4,5,6。
x = tf.Variable(1.0) y = 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,但是这种情况会出问题
讯享网
讯享网x = tf.Variable(1.0) y = 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())
转载信息:
作者:hu_guan_jie
来源:CSDN
原文:https://blog.csdn.net/hu_guan_jie/article/details/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/71372.html