您的位置:首页 > 其它

Tensorflow 解惑[一]:关于tf.identity 和tf.control_dependencies

2018-04-02 19:23 477 查看

Tensorflow 解惑[一]:关于tf.identity 和tf.control_dependencies

最近在stackoverflow上看到一个问题,链接是In TensorFlow, what is tf.identity used for?。最高票的答案贴了两段代码,说明了使用tf.identity后,才会得到累加的效果,但并未解释其中原因,这篇文章做一些解释,有不对的地方还请大神指点。
首先,从tensor的定义说起,官网的解释为:A tensor is a generalization of vectors and matrices to potentially higher dimensions. tensor可以是Variable,Constant,Placeholder等等。但是,官网上还有一句话值得注意:Unlike tf.Tensor objects, a tf.Variable exists outside the context of a single session.run call.这个说明了variable的特殊性,这也就是 y = x 失效,而 y = tf.identity(x)有效的原因。同时,对于tf.control_dependencies,官网上有这么句话:control_inputs: A list of Operation or Tensor objects which must be executed or computed before running the operations defined in the context. 继续看下面的代码:
import tensorflow as tf
x = tf.Variable(0.0)
print x
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
y = x
print y
#z=tf.identity(x,name='x')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(sess.run(y))
运行的结果为:
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>
2018-01-06 17:44:49.511158: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
0.0
0.0
0.0
0.0
0.0
此时y的类型为tf.Variable,不受tf.control_dependencies控制。我们把y=x换成y=x+0.0,代码变为:
import tensorflow as tf
x = tf.Variable(0.0)
print x
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
y = x + 0.0
print y
#z=tf.identity(x,name='x')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(sess.run(y))
再次运行代码,输出为:
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>
Tensor("add:0", shape=(), dtype=float32)
2018-01-06 18:01:28.295217: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
1.0
2.0
3.0
4.0
5.0
此时,y的类型变成了tensor类型,受tf.control_dependencies的约束,生成y之前,进行了5次自加操作。最后,换成y = tf.identity(x,name='x')。代码如下:
import tensorflow as tf
x = tf.Variable(0.0)
print x
x_plus_1 = tf.assign_a
c41a
dd(x, 1)

with tf.control_dependencies([x_plus_1]):
y = tf.identity(x,name='x')
print y
#z=tf.identity(x,name='x')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(sess.run(y))
运行代码,输出为:
/Users/xuyao/anaconda3/envs/python/bin/python /Users/xuyao/PycharmProjects/Test/test_tf_identity.py
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>
2018-01-06 18:51:58.121838: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
Tensor("x:0", shape=(), dtype=float32)
1.0
2.0
3.0
4.0
5.0
tf.identity属于tensorflow中的一个ops,跟x = x + 0.0的性质一样,返回一个tensor,受到tf.control_dependencies的约束,所以生效。
至此,文章开头提到的困惑就可以解答了。

作者:phusFuNs
链接:https://www.jianshu.com/p/6304b7c7896b
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: