关于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_add(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的约束,所以生效。




转自:https://www.jianshu.com/p/6304b7c7896b

目录
相关文章
|
算法框架/工具
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
|
4月前
|
TensorFlow API 算法框架/工具
【Tensorflow】解决Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Te
文章讨论了在使用Tensorflow 2.3时遇到的一个错误:"Inputs to eager execution function cannot be Keras symbolic tensors...",这个问题通常与Tensorflow的eager execution(急切执行)模式有关,提供了三种解决这个问题的方法。
42 1
|
4月前
|
TensorFlow 算法框架/工具
【Tensorflow】解决A `Concatenate` layer should be called on a list of at least 2 inputs
在TensorFlow 2.0中,使用Concatenate函数时出现错误,可以通过替换为tf.concat 来解决。
44 4
|
7月前
|
机器学习/深度学习 人工智能
【CatBoost报错解决】CatBoostError: Bad value for num feature[non default doc idx=0,feature idx=19]=
【CatBoost报错解决】CatBoostError: Bad value for num feature[non default doc idx=0,feature idx=19]=
|
API 数据格式
TensorFlow2._:model.summary() Output Shape为multiple解决方法
TensorFlow2._:model.summary() Output Shape为multiple解决方法
281 0
TensorFlow2._:model.summary() Output Shape为multiple解决方法
|
自然语言处理 搜索推荐 数据挖掘
RolePred: Open-Vocabulary Argument Role Prediction for Event Extraction 论文解读
事件抽取中的论元角色是指事件和参与事件的论元之间的关系。尽管事件抽取取得了巨大进展,但现有研究仍然依赖于领域专家预定义的角色。
72 0
|
TensorFlow API 算法框架/工具
解决AttributeError: module ‘keras.utils‘ has no attribute ‘plot_model‘
解决AttributeError: module ‘keras.utils‘ has no attribute ‘plot_model‘
330 0
解决AttributeError: module ‘keras.utils‘ has no attribute ‘plot_model‘
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
【解决方案】The opset version of the onnx model is 12, only model with opset_version 10/11 is supported.
【解决方案】The opset version of the onnx model is 12, only model with opset_version 10/11 is supported.
770 0
|
算法框架/工具 Windows