tf.control_dependencies与tf.identity组合详解

简介:


tf.control_dependencies(self, control_inputs)

arguments:control_inputs: A list of `Operation` or `Tensor` objects which must be executed or computed before running the operations defined in the context. (注意这里control_inputs是list)
return:  A context manager that specifies control dependencies for all operations constructed within the context.(返回所有在环境中的控制依赖的上下文管理器)

该方法可以控制操作(op)执行的顺序,不能为tensor

tf.identity(input, name=None) 

Args:
input: A Tensor.
name: A name for the operation (optional).

Returns:A tensor with the same shape and contents as the input tensor or value.


源于StackOverFlow有个关于两者使用的例子:

[python]  view plain  copy
  1. x = tf.Variable(0.0)  
  2. x_plus_1 = tf.assign_add(x, 1)  
  3.   
  4. with tf.control_dependencies([x_plus_1]):  
  5.     y = x  
  6. init = tf.global_variables_initializer()  
  7.   
  8. with tf.Session() as session:  
  9.     init.run()  
  10.     for i in range(5):  
  11.         print(y.eval())  


针对此程序,输出结果为:0.0 0.0 0.0 0.0 0.0

输出变量x,结果也为0.0

说明x_plus_1操作并没有被执行,我认为虽然tf.control_dependencies参数中的op列表会在with包含的操作op执行之前先执行,但是y=x这个语句并不是一个op,而是一个tensor,所以执行y=x时,并不会执行tf.control_dependencies参数中的操作op。

所以可以将  y=x 修改为 y=tf.identity(x),此时这个语句就是一个操作op,要先执行tf.control_dependencies参数中的op列表,再执行y=tf.identity(x)操作,最终输出结果为1.0 2.0 3.0 4.0 5.0,最终变量x的结果也为5.0,完整程序如下:

[python]  view plain  copy
  1. x = tf.Variable(0.0)  
  2. x_plus_1 = tf.assign_add(x, 1)  
  3.   
  4. with tf.control_dependencies([x_plus_1]):  
  5.     y = tf.identity(x)  
  6. init = tf.global_variables_initializer()  
  7. with tf.Session() as session:  
  8.     init.run()  
  9.     for i in range(5):  
  10.         print(y.eval())  
  11.     print(x.eval()) 
from:http://blog.csdn.net/winycg/article/details/78820032
目录
相关文章
|
2月前
|
TensorFlow 算法框架/工具
【Tensorflow】解决A `Concatenate` layer should be called on a list of at least 2 inputs
在TensorFlow 2.0中,使用Concatenate函数时出现错误,可以通过替换为tf.concat 来解决。
24 4
|
2月前
|
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(急切执行)模式有关,提供了三种解决这个问题的方法。
28 1
|
API 数据格式
TensorFlow2._:model.summary() Output Shape为multiple解决方法
TensorFlow2._:model.summary() Output Shape为multiple解决方法
255 0
TensorFlow2._:model.summary() Output Shape为multiple解决方法
|
机器学习/深度学习 数据挖掘
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
127 0
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
|
算法框架/工具 Windows
|
机器学习/深度学习 TensorFlow 算法框架/工具
TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable
TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable
TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable
|
并行计算
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the
503 0
|
机器学习/深度学习 BI TensorFlow
TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例
TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例
|
IDE Linux 编译器
hostguest nativelangsys及uniform cui cross compile system
本文关键字:windows host targetting at linux,Compile for linux on windows using mingw64,Cross-compiling on Windows for Linux
199 0
hostguest nativelangsys及uniform cui cross compile system