参考链接:

http://blog.csdn.net/m0_37041325/article/details/76943364

https://stackoverflow.com/questions/34877523/in-tensorflow-what-is-tf-identity-used-for


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有个关于两者使用的例子:

x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)with tf.control_dependencies([x_plus_1]):y = x
init = tf.global_variables_initializer()
with tf.Session() as session:init.run()for i in range(5):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,完整程序如下:

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.global_variables_initializer()
with tf.Session() as session:init.run()for i in range(5):print(y.eval())print(x.eval())

tf.control_dependencies与tf.identity组合详解相关推荐

  1. 【TensorFlow】理解tf.nn.conv2d方法 ( 附代码详解注释 )

    最近在研究学习TensorFlow,在做识别手写数字的demo时,遇到了tf.nn.conv2d这个方法,查阅了官网的API 发现讲得比较简略,还是没理解.google了一下,参考了网上一些朋友写得博 ...

  2. tensorflow学习笔记:tf.control_dependencies,tf.GraphKeys.UPDATE_OPS,tf.get_collection

    tf.control_dependencies(control_inputs): control_dependencies(control_inputs) ARGS: control_inputs:在 ...

  3. 【转】tensorflow中的batch_norm以及tf.control_dependencies和tf.GraphKeys.UPDATE_OPS的探究

    笔者近来在tensorflow中使用batch_norm时,由于事先不熟悉其内部的原理,因此将其错误使用,从而出现了结果与预想不一致的结果.事后对其进行了一定的调查与研究,在此进行一些总结. 一.错误 ...

  4. tf.expand_dims()和tf.squeeze()的用法详解

    tf.expand_dims tf.expand_dims(input, axis=None, name=None, dim=None ) 给定的张量input,axis为需要在第几维度扩充,axis ...

  5. torch.nn.identity()方法详解

    先看代码 m = nn.Identity( 54, unused_argument1=0.1, unused_argument2=False )input = torch.randn(128, 20) ...

  6. tf.cancat() 详解 —》理解为主

    tensorflow 中用来拼接张量的函数tf.concat(),用法: tf.concat([tensor1, tensor2,...], axis) 代码详解-维度变化理解: # 参考:https ...

  7. 【神经网络架构】Swin Transformer细节详解-1

    目录 源码: 1. Patch Partition + Liner Embedding 模块 2. Swin Transformer block(一个完整的W-MSA) partition windo ...

  8. linux压缩解压缩文件夹或文件命令详解

    一.压缩当前目录下的文件夹或者文件app到app.tar tar -cvf app.tar app 二.解压缩当前目录下的压缩包app.tar到app tar -xvf app.tar 三.不解压的情 ...

  9. python 3.x 爬虫基础---http headers详解

    python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---Requer ...

最新文章

  1. Pwntools---fmtstr_payload()介绍
  2. 卡方 python_Python评分卡建模—卡方分箱
  3. iOS原生WebView中JavaScript和OC交互
  4. Javascript实现AES加密解密(ECB/CBC)
  5. python 把4个二进制组成float_4个方法用Python自由定制Excel表格
  6. Springboot中拦截器的使用
  7. OrCAD(一)简介、安装与基本使用
  8. vue实现点击按钮“查看详情”弹窗展示详情列表
  9. CAD二次开发--->批量快速创建对齐标注插件
  10. 合工大OJ 1359
  11. that was originally added here
  12. linux6 下dns配置,RHEL6中DNS配置
  13. git提交规范图-提问的智慧图谱-React 学习路线图- 达克效应
  14. KerberosSDR代码笔记(5) 信号处理(采样时间延迟计算、相位差计算的2种方法、MUSIC算法)
  15. 主成分分析(PCA)原理及推导
  16. python爬取虎扑评论_Python爬取NBA虎扑球员数据
  17. CBAM CBAM: Convolutional Block Attention Module
  18. 【Linux】-- 操作系统进程的状态
  19. TypeScript免费视频图文教程(2W字)
  20. 数据中心如何向私有云转变

热门文章

  1. python求股票收益率_python – 有没有办法自动获取许多股票的一般信息,如市盈率,收益率等等?...
  2. 服务器无法显示url,遇到“您所请求的网址(URL)无法获取”的解决办法
  3. 追风人拍到壮观龙卷风
  4. Java开源生鲜电商平台-深刻理解电商的库存架构与解决方案(源码可下载)
  5. 浏览器访问百度www.baidu.com过程
  6. ASP无限分类数据库版
  7. Unity打包exe文件流程
  8. ★为什么不要和“穷人”做朋友?
  9. Swift 中的类与结构体
  10. 六脚自锁开关 引脚定义